[BUG] Parallel subagents with worktree isolation fail due to git config lock contention

Resolved 💬 8 comments Opened Mar 15, 2026 by WeMingT Closed Apr 17, 2026

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

  1. Open Claude Code in any git repository
  2. Send a prompt that triggers 3 parallel Agent tool calls, each with isolation: "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.
  3. Alternatively, explicitly request: "Launch 3 parallel subagents with worktree isolation to research X, Y, and Z"
  4. Observe that 1-2 of the 3 agents fail with the .git/config.lock error above.
  5. Run git branch to see orphaned worktree-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 add calls 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 add fails 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-xxx

View original on GitHub ↗

This issue has 8 comments on GitHub. Read the full discussion on GitHub ↗