[BUG] Parallel subagents with worktree isolation fail due to git config lock contention
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
When launching 3+ Agent tool calls in parallel with isolation: "worktree", concurrent git worktree add commands race for the .git/config.lock file, causing one or more agents to fail immediately.
The failed agent produces the error and never executes its task. Additionally, an orphaned git branch (worktree-agent-XXXXXXXX) is left behind and requires manual cleanup.
What Should Happen?
All parallel worktree-isolated subagents should be created successfully without lock contention. The worktree creation should be serialized internally, or include retry/backoff logic to handle concurrent .git/config.lock access.
Error Messages/Logs
Failed to create worktree: Preparing worktree (new branch 'worktree-agent-a15ce112')
error: could not lock config file .git/config: File exists
error: unable to write upstream branch configuration
hint:
hint: After fixing the error cause you may try to fix up
hint: the remote tracking information by invoking:
hint: git branch --set-upstream-to=origin/refs/heads/main
Steps to Reproduce
- Open Claude Code in any git repository
- Send a prompt that triggers 3 parallel
Agenttool calls, each withisolation: "worktree". For example, ask Claude to "explore these 3 parts of the codebase in parallel" — Claude will launch 3 Explore subagents simultaneously with worktree isolation. - Alternatively, explicitly request: "Launch 3 parallel subagents with worktree isolation to research X, Y, and Z"
- Observe that 1-2 of the 3 agents fail with the
.git/config.lockerror above. - Run
git branchto see orphanedworktree-agent-*branches left behind.
Note: Running a single worktree agent works fine. The issue only occurs with 2+ concurrent worktree creations.
Claude Model
Opus
Is this a regression?
Yes, this worked in a previous version
Last Working Version
Uncertain of exact version, but this started occurring in recent v2.1.x releases (approximately within the last 1-2 weeks).
Claude Code Version
2.1.76
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
Windows Terminal
Additional Information
Root Cause: Multiple git worktree add commands execute simultaneously, each needing to write to .git/config. Git uses .git/config.lock for mutual exclusion, so concurrent writes cause lock contention.
Suggested Fixes:
- Option A (Recommended): Serialize
git worktree addcalls with an internal mutex/queue, so concurrent agent launches don't race on.git/config. - Option B: Add retry logic with exponential backoff when
git worktree addfails due to lock contention. - Option C: For read-only agent types (e.g.,
Explore), skip worktree isolation entirely since they don't modify files.
Current Workaround:
rm -f .git/config.lock
git worktree prune
git branch -D worktree-agent-xxxThis issue has 8 comments on GitHub. Read the full discussion on GitHub ↗