Windows: worktree removal fails with Permission denied due to MCP shutdown ordering

Resolved 💬 3 comments Opened Mar 10, 2026 by jun2077681 Closed Apr 7, 2026

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 remove fails with Permission 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

  1. Windows 11, Claude Code with MCP servers configured (e.g., chrome-devtools, codex-cli)
  2. Run claude --debug --verbose -w work4
  3. Inside the session, type /exit (choose "Remove")
  4. Check debug log — Permission denied error before MCP SIGINT
  5. Empty work4 directory 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:

  1. Reorder shutdown: Shut down MCP servers → then remove worktree
  2. Retry after MCP shutdown: Attempt worktree removal, if fails, retry after MCP servers exit
  3. Fix log message: At minimum, don't log "cleaned up completely" when removal failed

View original on GitHub ↗

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