Skip to content

Environment teardown retries forever after an interrupted hook, with no recovery path #5960

Closed

Description

@brsbl

Problem

A worktree environment whose setup hook was interrupted can never finish teardown. It retries every minute, indefinitely, and no supported command recovers it.

Seen on a Linux host: a git-worktree environment of an archived thread, status error, lifecycle.teardown failed, attempt 7,873 since about Oct 15 2026. Each attempt fails with:

Environment hook outcome is unknown after interruption. Automatic cleanup is blocked; inspect the workspace before recovering it.

bb environment cleanup <environment-id> returns {"ok": true} and changes nothing. That holds even after the workspace was inspected and removed by hand: the tree was clean, HEAD d722659a was already on origin/main, and the worktree was removed with git worktree remove. The message tells you to "recover" the environment, but there is no recover command (bb environment --help, bb guide).

Cause (at ab04f04)

  • When removal follows a cancellation, environment-engine.ts:896 calls cancelPendingEnvironmentHook for the …:setup operation, whose environmentHookOperations row was never finished.
  • After the interruption (for example a daemon restart), the host daemon no longer has that operation in memory. apps/host-daemon/src/command-handlers/environment-hook.ts:106-108 then returns { status: "unknown" }.
  • apps/server/src/services/environments/environment-hooks.ts:172-175 throws on unknown before it marks the row finished, so every retry hits the same throw.

"Unknown" covers two different cases. A daemon that never heard of the operation (or restarted) cannot have a hook still running. A hook whose termination really couldn't be verified (terminationUnverified) might be. Today both cases are treated as an indefinite block.

Expected

  1. If the operation isn't known to the current daemon instance, and that daemon started after the operation's startedAt, record the hook as finished/interrupted and continue teardown. A daemon boot ID or a start-time comparison would make that safe.
  2. Keep the block only for a real terminationUnverified outcome. Even then, offer a supported recovery after inspection: bb environment cleanup <id> --recover, or a documented recovery step, that marks the hook finished and retries removal.
  3. Stop the per-minute retry loop once a teardown is blocked on manual recovery: back off, or move to a terminal "needs recovery" state that bb environment list shows.

Tests

  • A server test where the daemon cancel returns unknown for an operation unknown to a daemon that restarted after startedAt fails today and should pass. Teardown should then complete.
  • terminationUnverified still blocks automatic cleanup, and the recovery command unblocks it.

Safety note

Production processes can look tied to such a worktree when they aren't. pnpm hard-links the store, so a production .node file and esbuild share inodes with files under the worktree's node_modules (link counts 59 and 115). /proc/<pid>/maps and exe resolve to the production install, so removing the worktree only lowers link counts. Any cleanup tooling should judge "in use" by path in /proc/<pid>/maps and cwd, not by inode.

Activity

  1. added theissue type on Oct 21, 2026
  2. added
    workspaces
    on Oct 21, 2026
  3. brsbl commented on Oct 21, 2026

    @brsbl
    CollaboratorAuthor

    The restart-safety assumption is disproved on current main (405592c0b71b1655a6ab2e1cc7498b5beeb78caa, Linux).

    environment-lifecycle-script.ts:217–223 launches hooks in detached process groups. In an owned, isolated fixture, I invoked the actual runSetupScript, waited for its script to record its PID, then sent SIGKILL only to the invoking Node process. The hook remained alive:

    {"runnerPid":3346871,"hookPid":3346908}
    CONFIRMED: actual runSetupScript process remains alive after its owner exits
    Name: sleep
    State: S (sleeping)
    Pid: 3346908
    PPid: 3346740
    

    The hook had been reparented to the fixture supervisor. I then killed only that recorded fixture PID. No production process or environment was inspected or changed.

    There is a second relevant case: current setup executes through RuntimeManager.createEntry, outside the hook RPC registry. Cancellation can therefore encounter no hook-operation row at all.

    Approved revised scope for #5961: preserve blocking of ambiguous outcomes; bound retries with backoff and expose a terminal “Needs recovery” message; add explicit cleanup --recover after inspection. Recovery must refuse workspace cwd/mapped-path users and unreadable processes owned by the daemon UID. Other users’ processes require separate inspection. A newer daemon instance alone will not authorize automatic deletion. Durable process ownership and automatic restart recovery will be tracked separately.

    BB-Thread-ID: thr_7y7sh3cxeb

    AGENT GENERATED

  4. added
    confirmed-repro
    on Oct 21, 2026
  5. bb-slop-cop commented on Oct 21, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority Medium · Effort High · workspaces.
    • Report: Reproduction and root-cause analysis — REPRODUCED, high root-cause confidence; confirmed-repro applied.
    • Evidence: Repeated by the same agent in two clean trusted-main checkouts. Three teardown attempts retain the pending hook and reset the 60-second delay; cleanup accepts another attempt without progress. The terminated control passes; 66 existing server tests and 10 daemon tests also pass.
    • Root cause: An ambiguous cancellation throws before finishing the durable hook record, while cleanup only requeues the same operation. A daemon restart alone does not establish that detached hook processes exited.
    • No duplicate PR: #5961 already links this issue. Safe recovery also requires protocol/product-policy work outside this rule's simple-fix scope. Related existing report: An interrupted teardown hook leaves a managed environment retrying forever, with no backoff and no recovery command #4158.

    AGENT GENERATED

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

    confirmed-reproworkspaces

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions