git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3] crendential-store: use timeout when locking file

From
Simão Afonso <simao.afonso@powertools-tech.com>
Date
Nov 25, 2020, 18:31 UTC
Message-ID
<20201125183123.47praa6hcgrgrib4@safonso-t430>
In-Reply-To
<xmqq4kleif26.fsf@gitster.c.googlers.com>

When holding the lock for rewriting the credential file, use a timeout to avoid race conditions when the credentials file needs to be updated in parallel.

An example would be doing `fetch --all` on a repository with several remotes that need credentials, using parallel fetching.

The timeout can be configured using "credentialStore.lockTimeoutMS", defaulting to 1 second.

Signed-off-by: Simão Afonso <simao.afonso@powertools-tech.com>
---
lockTimeoutMS sounds like the consensual name, implemented.
 Documentation/config/credential.txt | 6 ++++++
 builtin/credential-store.c          | 8 ++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/Documentation/config/credential.txt b/Documentation/config/credential.txt
index 9d01641c2..512f31876 100644
--- a/Documentation/config/credential.txt
+++ b/Documentation/config/credential.txt
@@ -28,3 +28,9 @@ credential.<url>.*::
 
 credentialCache.ignoreSIGHUP::
 	Tell git-credential-cache--daemon to ignore SIGHUP, instead of quitting.
+
+credentialStore.lockTimeoutMS::
+	The length of time, in milliseconds, for git-credential-store to retry
+	when trying to lock the credentials file. Value 0 means not to retry at
+	all; -1 means to try indefinitely. Default is 1000 (i.e., retry for
+	1s).
diff --git a/builtin/credential-store.c b/builtin/credential-store.c
index 5331ab151..ae3c1ba75 100644
--- a/builtin/credential-store.c
+++ b/builtin/credential-store.c
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "credential.h"
 #include "string-list.h"
@@ -58,8 +59,11 @@ static void print_line(struct strbuf *buf)
 static void rewrite_credential_file(const char *fn, struct credential *c,
 				    struct strbuf *extra)
 {
-	if (hold_lock_file_for_update(&credential_lock, fn, 0) < 0)
-		die_errno("unable to get credential storage lock");
+	int timeout_ms = 1000;
+
+	git_config_get_int("credentialstore.locktimeoutms", &timeout_ms);
+	if (hold_lock_file_for_update_timeout(&credential_lock, fn, 0, timeout_ms) < 0)
+		die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms);
 	if (extra)
 		print_line(extra);
 	parse_credential_file(fn, c, NULL, print_line);
-- 
2.29.2
Previous: Jeff KingNext: Jeff King
Message 6 of 7 in “credential-store: use timeout when locking file”
  1. credential-store: use timeout when locking fileSimão Afonso, Oct 30, 2020
  2. Junio C HamanoOct 30, 2020
  3. crendential-store: use timeout when locking fileSimão Afonso, Nov 24, 2020
  4. Junio C HamanoNov 24, 2020
  5. Jeff KingNov 25, 2020
  6. crendential-store: use timeout when locking fileSimão Afonso, Nov 25, 2020
  7. Jeff KingNov 26, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.