Skip to content

Commit 1526f65

Browse files
committed
git_config_set_multivar_in_file_gently(): add a lock timeout
In particular when multiple processes want to write to the config simultaneously, it would come in handy to not fail immediately when another process locked the config, but to gently try again. This will help with Scalar's functional test suite which wants to register multiple repositories for maintenance semi-simultaneously. As not all code paths calling this function read the config (e.g. `git config`), we have to read the config setting via `git_config_get_ulong()`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2d2d322 commit 1526f65

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Documentation/config/core.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,12 @@ core.WSLCompat::
777777
The default value is false. When set to true, Git will set the mode
778778
bits of the file in the way of wsl, so that the executable flag of
779779
files can be set or read correctly.
780+
781+
core.configWriteLockTimeoutMS::
782+
When processes try to write to the config concurrently, it is likely
783+
that one process "wins" and the other process(es) fail to lock the
784+
config file. By configuring a timeout larger than zero, Git can be
785+
told to try to lock the config again a couple times within the
786+
specified timeout. If the timeout is configure to zero (which is the
787+
default), Git will fail immediately when the config is already
788+
locked.

config.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,7 @@ int repo_config_set_multivar_in_file_gently(struct repository *r,
31823182
const char *comment,
31833183
unsigned flags)
31843184
{
3185+
static unsigned long timeout_ms = ULONG_MAX;
31853186
int fd = -1, in_fd = -1;
31863187
int ret;
31873188
struct lock_file lock = LOCK_INIT;
@@ -3202,11 +3203,16 @@ int repo_config_set_multivar_in_file_gently(struct repository *r,
32023203
if (!config_filename)
32033204
config_filename = filename_buf = repo_git_path(r, "config");
32043205

3206+
if ((long)timeout_ms < 0 &&
3207+
git_config_get_ulong("core.configWriteLockTimeoutMS", &timeout_ms))
3208+
timeout_ms = 0;
3209+
32053210
/*
32063211
* The lock serves a purpose in addition to locking: the new
32073212
* contents of .git/config will be written into it.
32083213
*/
3209-
fd = hold_lock_file_for_update(&lock, config_filename, 0);
3214+
fd = hold_lock_file_for_update_timeout(&lock, config_filename, 0,
3215+
timeout_ms);
32103216
if (fd < 0) {
32113217
error_errno(_("could not lock config file %s"), config_filename);
32123218
ret = CONFIG_NO_LOCK;

0 commit comments

Comments
 (0)