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.