Skip to content

Commit 7ea6add

Browse files
committed
maintenance: delete stale lock files
The maintenance.lock file exists to prevent concurrent maintenance processes from writing to a repository at the same time. However, it has the downside of causing maintenance to start failing without recovery if there is any reason why the maintenance command failed without cleaning up the lock-file. This change makes it such that maintenance will delete a lock file that was modified over 6 hours ago. This will auto-heal repositories that are stuck with failed maintenance (and maybe it will fail again, but we will get a message other than the lock file exists). Signed-off-by: Derrick Stolee <[email protected]>
1 parent e1487fa commit 7ea6add

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

builtin/gc.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts)
12841284
char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
12851285

12861286
if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1287+
struct stat st;
1288+
struct strbuf lock_dot_lock = STRBUF_INIT;
12871289
/*
12881290
* Another maintenance command is running.
12891291
*
@@ -1294,6 +1296,25 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts)
12941296
if (!opts->auto_flag && !opts->quiet)
12951297
warning(_("lock file '%s' exists, skipping maintenance"),
12961298
lock_path);
1299+
1300+
/*
1301+
* Check timestamp on .lock file to see if we should
1302+
* delete it to recover from a fail state.
1303+
*/
1304+
strbuf_addstr(&lock_dot_lock, lock_path);
1305+
strbuf_addstr(&lock_dot_lock, ".lock");
1306+
if (lstat(lock_dot_lock.buf, &st))
1307+
warning_errno(_("unable to stat '%s'"), lock_dot_lock.buf);
1308+
else {
1309+
if (st.st_mtime < time(NULL) - (6 * 60 * 60)) {
1310+
if (unlink(lock_dot_lock.buf))
1311+
warning_errno(_("unable to delete stale lock file"));
1312+
else
1313+
warning(_("deleted stale lock file"));
1314+
}
1315+
}
1316+
1317+
strbuf_release(&lock_dot_lock);
12971318
free(lock_path);
12981319
return 0;
12991320
}

t/t7900-maintenance.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ test_expect_success 'run [--auto|--quiet]' '
5252
test_subcommand git gc --no-quiet <run-no-quiet.txt
5353
'
5454

55+
test_expect_success 'lock file behavior' '
56+
test_when_finished git config --unset maintenance.commit-graph.schedule &&
57+
git config maintenance.commit-graph.schedule hourly &&
58+
59+
touch .git/objects/maintenance.lock &&
60+
git maintenance run --schedule=hourly --no-quiet 2>err &&
61+
grep "lock file .* exists, skipping maintenance" err &&
62+
63+
test-tool chmtime =-22000 .git/objects/maintenance.lock &&
64+
git maintenance run --schedule=hourly --no-quiet 2>err &&
65+
grep "deleted stale lock file" err &&
66+
test_path_is_missing .git/objects/maintenance.lock &&
67+
68+
git maintenance run --schedule=hourly 2>err &&
69+
test_must_be_empty err
70+
'
71+
5572
test_expect_success 'maintenance.auto config option' '
5673
GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
5774
test_subcommand git maintenance run --auto --quiet <default &&

0 commit comments

Comments
 (0)