Removing a worktree leaves behind any long-running processes that were started inside it — dev servers, watchers, language servers, tmux panes spawned by a post-start hook. wt remove deletes the directory (or trashes it) but the processes keep running, holding ports and file handles and, on the trash path, referencing files that no longer exist at their old path.
Would it be feasible for wt to reap those processes, perhaps as an option on wt remove?
The interesting part is identifying which processes "belong to" the worktree, since that's where I'd expect the feasibility to be decided:
- By working directory — processes whose cwd is under the worktree path (
lsof -a -d cwd +D <path> on macOS, /proc/*/cwd on Linux). Catches most dev servers, but misses a process that started in the worktree and then chdir'd away, and a daemon that forked and re-parented to init.
- By process group / session — if
wt recorded the pgid of hook-spawned processes at post-start, it could signal that group at remove. More reliable for things wt itself launched, but nothing for processes the user started by hand in the worktree.
- By tmux/zellij session — for the common case where a
post-start hook opens a multiplexer session named after the worktree, "reaping" might just mean killing that session.
Open questions:
- Is cwd-based detection reliable enough to reap by default, or would it need to be opt-in given the risk of killing something unrelated?
- Should this build on the existing
pre-remove hook (letting the user supply the kill logic) rather than wt implementing process discovery itself? A pre-remove hook already has the worktree path and can run whatever the user wants — so this might be a docs/example question rather than a code one.
- How should it interact with the trash-vs-delete path and the
--force flag?
Mostly asking whether this is in scope for wt at all, versus something better left to a user-supplied pre-remove hook.
This was written by Claude Code on behalf of max
Removing a worktree leaves behind any long-running processes that were started inside it — dev servers, watchers, language servers, tmux panes spawned by a
post-starthook.wt removedeletes the directory (or trashes it) but the processes keep running, holding ports and file handles and, on the trash path, referencing files that no longer exist at their old path.Would it be feasible for
wtto reap those processes, perhaps as an option onwt remove?The interesting part is identifying which processes "belong to" the worktree, since that's where I'd expect the feasibility to be decided:
lsof -a -d cwd +D <path>on macOS,/proc/*/cwdon Linux). Catches most dev servers, but misses a process that started in the worktree and thenchdir'd away, and a daemon that forked and re-parented to init.wtrecorded the pgid of hook-spawned processes atpost-start, it could signal that group atremove. More reliable for thingswtitself launched, but nothing for processes the user started by hand in the worktree.post-starthook opens a multiplexer session named after the worktree, "reaping" might just mean killing that session.Open questions:
pre-removehook (letting the user supply the kill logic) rather thanwtimplementing process discovery itself? Apre-removehook already has the worktree path and can run whatever the user wants — so this might be a docs/example question rather than a code one.--forceflag?Mostly asking whether this is in scope for
wtat all, versus something better left to a user-suppliedpre-removehook.