Windows: worktree removal fails with Permission denied due to MCP shutdown ordering
Bug Description
On Windows, git worktree remove fails with Permission denied during worktree session exit because MCP server processes are still running and holding CWD locks on the worktree directory. The error is silently ignored and logged as "Linked worktree cleaned up completely", leaving behind empty directories.
Root Cause
The shutdown sequence removes the worktree before shutting down MCP servers:
1. git worktree remove → Permission denied (MCP servers hold CWD lock)
2. "Linked worktree cleaned up completely" ← misleading, removal actually failed
3. MCP SIGINT sent ← too late, worktree removal already failed
4. MCP servers exit ← directory is now unlockable, but nothing retries deletion
Debug Log Evidence
07:22:34.082 [ERROR] Failed to remove linked worktree: error: failed to delete
'C:/.../worktrees/work4': Permission denied
07:22:34.394 [DEBUG] Deleted worktree branch: worktree-work4
07:22:34.394 [DEBUG] Linked worktree cleaned up completely ← misleading
07:22:34.399 [DEBUG] MCP server "chrome-devtools": Sending SIGINT ← after removal attempt
07:22:34.400 [DEBUG] MCP server "codex-cli": Sending SIGINT
07:22:34.593 [DEBUG] MCP server "chrome-devtools": MCP server process exited cleanly
07:22:34.593 [DEBUG] MCP server "codex-cli": MCP server process exited cleanly
This was reproduced with default worktree behavior (no custom WorktreeCreate/WorktreeRemove hooks) — the issue is in Claude Code's internal shutdown ordering.
Expected Behavior
MCP servers should be shut down before attempting git worktree remove, so no process holds a CWD lock on the worktree directory. Alternatively, retry directory deletion after MCP shutdown completes.
Actual Behavior
git worktree removefails withPermission denied- Error is caught but logged as "cleaned up completely"
- Empty worktree directories remain on disk
- Git worktree metadata is cleaned up (branch deleted), but physical directory persists
Steps to Reproduce
- Windows 11, Claude Code with MCP servers configured (e.g., chrome-devtools, codex-cli)
- Run
claude --debug --verbose -w work4 - Inside the session, type
/exit(choose "Remove") - Check debug log —
Permission deniederror before MCP SIGINT - Empty
work4directory remains in.claude/worktrees/
Environment
- OS: Windows 11 (10.0.26200)
- Claude Code: 2.1.72
- Shell: Git Bash
- MCP servers: chrome-devtools, codex-cli
Related Issues
- #26725 (stale worktrees not cleaned up)
- #15211 (Windows: MCP server child processes not cleaned up on exit)
- #1935 (MCP servers not properly terminated on exit)
Suggested Fix
Either:
- Reorder shutdown: Shut down MCP servers → then remove worktree
- Retry after MCP shutdown: Attempt worktree removal, if fails, retry after MCP servers exit
- Fix log message: At minimum, don't log "cleaned up completely" when removal failed
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗