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

Re: [PATCH] credential-store: use timeout when locking file

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 30, 2020, 19:49 UTC
Message-ID
<xmqq361v334r.fsf@gitster.c.googlers.com>
In-Reply-To
<20201030180718.4i7txqkgye7r6pkb@safonso-t430>
Simão Afonso <simao.afonso@powertools-tech.com> writes:
Show 6 quoted lines
> 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.
OK.
> The timeout is hardcoded to 1 second, since this is just to solve a race
> condition.

It is unclear what this sentence wants to explain. How does "this is to solve a race" justifies the choice of "1 second" (as opposed to say 10 seconds or 0.5 second)? Or is this trying to justify why there is no configurability? If so, why is it OK to hardcode it if it is done to solve a race? Are we assuming certain maximum rate of operation that is "reasonable"?

> This was reported here:
> https://lore.kernel.org/git/20201029192020.mcri76ylbdure2o7@safonso-t430/
> ---
Missing sign-off.
cf. https://git-scm.com/docs/SubmittingPatches.html#sign-off
Show 14 quoted lines
>  builtin/credential-store.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/credential-store.c b/builtin/credential-store.c
> index 5331ab151..acff4abae 100644
> --- a/builtin/credential-store.c
> +++ b/builtin/credential-store.c
> @@ -58,8 +58,9 @@ 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");
> +	static long timeout = 1000;

Why "static"? It would make your code easier to follow if you limit use of "static" to only cases where you want to take advantage of the fact that the value previously left by the earlier call is seen by the next call, and/or the address of the variable must be valid even after the control returns to the caller.

I would understand if this were "const long timeout = 1000".

If this were an identifier with longer lifespan, I would have suggested to include some scale in the variable name (e.g. timeout_ms to clarify that it is counted in milliseconds), but it is just for this short function, so let's say "timeout" is just fine.

Show 5 quoted lines
> +	if (hold_lock_file_for_update_timeout(&credential_lock, fn, 0, timeout) < 0)
> +		die_errno("unable to get credential storage lock in %ld ms", timeout);
>  	if (extra)
>  		print_line(extra);
>  	parse_credential_file(fn, c, NULL, print_line);
Thanks.
Previous: Simão AfonsoNext: Simão Afonso
Message 2 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.