ExitWorktree(remove) leaves an empty directory and the branch when a background task started in the worktree is still running (Windows)
What's Wrong?
On Windows, ExitWorktree with action: "remove" cannot remove a worktree while a background Bash task started inside that worktree is still running. It returns "Exited worktree but could not remove it", followed by the path it kept.
By then git has deregistered the worktree and deleted its files, so an empty directory remains, held open by the background task's processes. The worktree's branch also remains.
What Should Happen?
ExitWorktree removes the directory and the branch, as it does when no background task is running. For example, it could stop the background tasks whose working directory is inside the worktree, ask before stopping them, or refuse and name them.
Error Messages/Logs
Sysinternals handle64.exe, run elevated on the leftover directory while the background task was running, lists these holders and no others:
bash.exe pid: 56356 type: File 54: <repo>\.claude\worktrees\husk-probe-bg
bash.exe pid: 35024 type: File 174: <repo>\.claude\worktrees\husk-probe-bg
sleep.exe pid: 57048 type: File 174: <repo>\.claude\worktrees\husk-probe-bg
The Claude Code process is not among them.
Steps to Reproduce
EnterWorktree(name: "husk-probe-bg").- Run
sleep 600with the Bash tool andrun_in_background: true. ExitWorktree(action: "remove", discard_changes: true).- It returns "Exited worktree but could not remove it".
- The directory exists and is empty.
git worktree listno longer lists it.- The branch
worktree-husk-probe-bgstill exists.
- Stop the background task with
TaskStop.rmdiron the directory then succeeds, andgit branch -d worktree-husk-probe-bgdeletes the branch.
Control: the same steps without step 2 return "Exited and removed worktree", and leave no directory and no branch.
Related
#92342 describes the same Windows failure where the session process itself holds the directory, for worktrees Claude Code did not create. Here the worktree was created by EnterWorktree, and the holders are the session's background task.
Environment
- Claude Code: reproduced on 2.1.282. 2.1.285 is installed but not re-tested; its changelog adds a time limit to background Bash commands (default 30 minutes), which bounds how long the directory stays held but does not change
ExitWorktree. - Regression: not known.
- OS: Windows 11 (10.0.26200). Git 2.35.1.windows.2. The Bash tool's shell is Git Bash.
- Model: Opus.
Plugin language servers hold the folder the same way, so ExitWorktree also leaves the directory behind after any edit in the worktree while an LSP plugin is enabled. Reproduced on 2.1.299 (Claude desktop app, Windows 10 22H2):
- Enable
pyright-lsp@claude-plugins-official. EnterWorktree(name: "lsp-probe").- Edit any
.pyfile. Claude Code startspyright-langserver.exewith the worktree as its working directory. ExitWorktree(action: "remove", discard_changes: true).
The worktree's files are deleted and git drops its registration, but the empty directory stays until the session ends, because pyright-langserver.exe and its python.exe children still have it as their working directory. ExitWorktree(action: "keep") followed by git worktree remove fails the same way with "Permission denied". Stopping the server frees the directory, and Claude Code starts it again on its next request.
2.1.286 changed /exit's Remove worktree to run after Claude Code stops the servers and shells it started there; ExitWorktree needs the same. I haven't retested this issue's background-task case on 2.1.299.
AI assistance: Claude Code (Claude Opus 5.5) reproduced this and drafted this comment.