Skip to content

Commit 023753e

Browse files
derrickstoleedscho
authored andcommitted
Merge pull request git-for-windows#468: maintenance: delete stale lock files, fix loose-objects task
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). The intention here is to get users out of a bad state without weakening the maintenance lock _too_ much. I'm open to other thoughts, including expanding the timeframe from 6 to 24 hours. --- The `fixup!` commit fixes the use of the `gvfs.sharedcache` config value around the loose-objects task. Specifically, the loose-objects step might fail because it is calling `git pack-objects {garbage}/pack/loose` which fails.
2 parents 0388a01 + 9138490 commit 023753e

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
@@ -1309,6 +1309,8 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts)
13091309
char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
13101310

13111311
if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1312+
struct stat st;
1313+
struct strbuf lock_dot_lock = STRBUF_INIT;
13121314
/*
13131315
* Another maintenance command is running.
13141316
*
@@ -1319,6 +1321,25 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts)
13191321
if (!opts->auto_flag && !opts->quiet)
13201322
warning(_("lock file '%s' exists, skipping maintenance"),
13211323
lock_path);
1324+
1325+
/*
1326+
* Check timestamp on .lock file to see if we should
1327+
* delete it to recover from a fail state.
1328+
*/
1329+
strbuf_addstr(&lock_dot_lock, lock_path);
1330+
strbuf_addstr(&lock_dot_lock, ".lock");
1331+
if (lstat(lock_dot_lock.buf, &st))
1332+
warning_errno(_("unable to stat '%s'"), lock_dot_lock.buf);
1333+
else {
1334+
if (st.st_mtime < time(NULL) - (6 * 60 * 60)) {
1335+
if (unlink(lock_dot_lock.buf))
1336+
warning_errno(_("unable to delete stale lock file"));
1337+
else
1338+
warning(_("deleted stale lock file"));
1339+
}
1340+
}
1341+
1342+
strbuf_release(&lock_dot_lock);
13221343
free(lock_path);
13231344
return 0;
13241345
}

t/t7900-maintenance.sh

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

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

0 commit comments

Comments
 (0)