Skip to content

Deleting a worktree environment leaves its processes running with a cwd that no longer exists #1769

Closed as not planned

Description

@MGrin

Summary

When a managed worktree environment is torn down, bb removes the directory and marks the environment destroyed, but does not kill the processes it started inside it. They keep running — still parented into the bb app's process tree — with a working directory that no longer exists.

Observed on bb 0.38.0, macOS 26. First measured on 0.37.0; the behaviour is unchanged.

Live example, right now

$ bb environment show env_9dwtzh9mtz --json
  status: destroyed
  path:   null

The directory is gone. Two of its processes are not:

PID    PPID   ELAPSED    RSS       COMMAND
43769  1215   12:46:10    17 MB    .../bb-app/host-daemon/...
43770  43769  12:46:10   150 MB    ~/.local/bin/claude --output-format stream-json …

Both report cwd = /Users/mgrin/.bb/worktrees/env_9dwtzh9mtz/dotfiles, which does not exist. The claude provider process has been alive for 12 hours 46 minutes after its environment was destroyed, holding 150 MB.

Why it matters — the same leak at scale

Measured on this machine on 2026-08-14, before a local sweeper was added:

removed environments still holding processes 40
live processes belonging to them 190
resident memory held 1.75 GB
swap 94%
kernel_task 57% CPU, on paging alone
load average ~20 on 10 cores

The discriminating detail: every process-holding environment was a deleted one. None of the 8 environments that still existed had orphans. So this is not "agents leave processes around" in general — it is specifically teardown not reaping.

The machine became unusable. It recovered only when the orphans were killed by hand.

Reproduction

  1. Create a managed worktree environment in a project (bb thread spawn --new-environment worktree).
  2. Let the thread start a long-lived child — a provider process is enough; a dev server is the obvious case.
  3. Delete the environment.
  4. lsof -a -d cwd -Fpn and look for processes whose cwd is under ~/.bb/worktrees/<env>/ where that path no longer exists. They are still running.

Workaround, for anyone hitting this

We run an hourly sweeper (10 * * * *, 72 runs so far) that finds processes whose cwd is inside a non-existent bb worktree and kills them. It keeps the count down — 2 orphans at the moment of writing rather than 190 — but it is a workaround: it cleans up after the leak, on a delay, and a freshly built machine without it inherits the original behaviour.

It also has to be careful to kill by PID rather than by pattern, because pattern-matching provider processes on a machine running many agents kills live sessions belonging to other threads.

Suggested fix

Kill the environment's process group on teardown, before or as part of removing the directory. If a hard kill is too blunt, a SIGTERM to the group with a short grace period and then SIGKILL would still be a large improvement over leaving them indefinitely.

A cheaper partial mitigation, if reaping is contentious: report the surviving PIDs from the delete command so the caller at least knows what was left behind.

Activity

  1. added theissue type on Aug 18, 2026
  2. added
    workspaces
    on Aug 18, 2026
  3. SawyerHood commented on Aug 18, 2026

    @SawyerHood
    Collaborator

    Duplicate of #1647. Closing in favor of the earlier report. Your measurements (40 destroyed environments, 190 live processes, 1.75 GB) are the best evidence for this bug, so I am cross-posting them there.

    AGENT GENERATED: by Claude Opus 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    workspaces

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions