Repository navigation
fix(sync): change detection no longer takes git's index lock - #3338
Merged
Merged
Conversation
colbymchenry
force-pushed
the
claude/issue-3295-git-optional-locks
branch
from
November 17, 2026 22:32
d5208be to
a999677
Compare
Change detection runs `git status --porcelain --no-renames -z -uall` with git's optional locks on. In that mode git writes the stat info it refreshed back to the index, so it takes .git/index.lock right after the refresh and holds it through the diff and the whole untracked scan. Every sync runs it twice (the watcher's and codegraph sync), codegraph status and the MCP freshness checks once each, and the watcher wakes exactly when a commit, a checkout or a pre-commit hook churns files. A git write that started inside that window failed at once with "index.lock: File exists". The status call, and the commit-range diff beside it, now run with GIT_OPTIONAL_LOCKS=0. That skips only the write-back: the output is the same, and the user's next git command refreshes the index itself. It is an environment variable rather than --no-optional-locks, which a git older than 2.15 rejects. The other git calls change detection makes (ls-files, rev-parse, cat-file, config) never write the index. Validation: - New git-status-optional-locks tests (2): getGitChangedFiles, a sync and a status check leave .git/index byte-identical. Both fail on main, where the index is rewritten, and pass with the fix. Each first checks that a plain git status in the same state does rewrite it. - A repo with 10,000 tracked and 10,000 untracked files, polling for .git/index.lock while the CLI runs: main's build took it in 12 of 12 syncs and 8 of 8 status runs, the fix in 0 of 12 and 0 of 8. - Published 1.6.3 on the same repo, with a git write loop running beside each sync: 3 of 176 writes failed with "index.lock: File exists", 0 of 175 with no sync running, and 0 of 178 with GIT_OPTIONAL_LOCKS=0 set. - Neighbouring suites (git-changed-untracked-dir, git-index-currency, git-status-subdir-project, git-ignored-dirs-scope, cli-sync): 37 passed. Fixes #3295 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry
force-pushed
the
claude/issue-3295-git-optional-locks
branch
from
November 17, 2026 23:17
a999677 to
2e12cd9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Change detection no longer takes git's index lock. A
git addin a pre-commit hook, a commit or a checkout that runs while CodeGraph syncs no longer fails withfatal: Unable to create '.git/index.lock': File exists.Root cause
collectGitStatus(src/extraction/index.ts) runsgit status --porcelain --no-renames -z -uallwith git's optional locks on. In that mode git writes the stat info it refreshed back to the index, so it takes.git/index.lockright after the refresh and holds it through the diff and the whole untracked scan. A sync runs it twice;codegraph statusand the MCP freshness checks run it once. The watcher wakes exactly when a commit, checkout or hook churns files, so the collisions cluster around the user's own git writes, as the issue's trace2 capture shows.Fix
The status call, and the commit-range
git diffbeside it, run withGIT_OPTIONAL_LOCKS=0. That skips only the write-back: the output is identical, and the user's next git command refreshes the index itself. It is an environment variable rather than--no-optional-locks, which git before 2.15 rejects. The other git calls in change detection (ls-files,rev-parse,cat-file,config) never write the index.Validation
__tests__/git-status-optional-locks.test.ts(2 tests):getGitChangedFiles, a sync and a status check leave.git/indexbyte-identical. Both fail on main (the index is rewritten) and pass with the fix. Each first checks that a plaingit statusin the same state does rewrite it, so the assertion can't pass vacuously..git/index.lockwhile the CLI runs (Windows 11, git for Windows): main's build took it in 12/12 syncs and 8/8statusruns; this branch in 0/12 and 0/8.git update-index --refresh) running beside each sync: 3 of 176 writes failed with "index.lock: File exists", 0 of 175 with no sync running, 0 of 178 withGIT_OPTIONAL_LOCKS=0set (what this branch sets).git-changed-untracked-dir,git-index-currency,git-status-subdir-project,git-ignored-dirs-scope,cli-sync: 37 passed.Fixes #3295
Thanks @iamclaudia-ai for the report, the trace2 capture and the suggested fix.
🤖 Generated with Claude Code