Skip to content

Stop a chat's background tools when the chat stops - #48089

Merged
brennanb2025 merged 42 commits into
mainfrom
brennanb2025/fix-provider-exec-setsid
Nov 25, 2026
Merged

brennanb2025 merged 42 commits into
mainfrom
brennanb2025/fix-provider-exec-setsid

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When you stopped an agent chat on a Mac or Linux, anything the agent had started in the background (a dev server, a headless browser, a search tool) could keep running forever with no owner. Over days these piled up and slowed the whole machine. Now stopping the chat also stops what it started, and nothing that isn't provably the chat's.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​115 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​115
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1410 $\color{#cf222e}{\Huge{\mathbf{−}}}$​56 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​1354

What Changed

  • native/provider-exec/provider-exec.c: the watcher used to signal and probe only the CLI's process group, and reported group-absent once that group was empty. Tools that call setsid (every shell tool call does) survived, and the TypeScript teardown skipped its own tree kill because the verdict was exited.
  • The watcher now reads the process table before it signals. It sends TERM, then KILL after the grace period, to every live member of the chat's tree. It reports group-absent only once that tree is gone too. On Linux that needs the subreaper's own children list to hold nothing but the watcher. On macOS a process that vanishes or can't be read during a sweep keeps the stop unconfirmed until a later sweep.
  • Tree membership:
    • Linux: everything under the provider-exec subreaper. The subreaper is pinned by a pidfd it opens before fork, and the sweep is refused if /proc belongs to another PID namespace.
    • macOS: the live CLI's descendants and orphans in a session one of them created. Also launchd orphans that carry this chat's random ORCA_PROVIDER_TREE env tag, are the same user and started after the CLI; those admit only themselves and their live children, never the rest of their session.
  • Every signal goes through a kernel handle taken before the start-time check: a pidfd on Linux, the audit token on macOS. A reused PID is never signalled.
  • Where the tree can't be proven, the stop reports unverified instead of claiming the tools are gone. If that can't change on the host (no pidfd, a /proc from another namespace), it reports at once, as fast as before. A hidepid mount or a failed read retries until the deadline.
  • Each tool gets TERM once. The watcher records who got it, and keeps retrying during the grace period for tools that appear late.
  • provider-ownership-journal.ts / provider-group-orphan-guard.ts: restart recovery no longer SIGKILLs a watcher that is still stopping tools; an unfinished watcher keeps its record for a later pass.

Why

Once a tool's shell exits, macOS reparents the tool to launchd and keeps no record of where it came from, so walking parent links at stop time can't find it. A per-chat environment cookie is the standard way process-tree cleanup finds such leftovers. Linux needs none of this, because the subreaper adopts them.

Linked Issue

N/A (internal report).

Visual Proof

N/A: no UI change; process-level behavior, proven by the test and QA below.

Testing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

  • provider-exec-tree-teardown.integration.test.ts builds the real binary. The CLI starts a shell in its own session that orphans a node process and an untagged sleep. After term, the node process must be gone, and an unrelated process must survive. The sleep must be gone on Linux and survive on macOS, since nothing proves it is the chat's there. Passes on Linux; fails with the previous provider-exec.c.

  • macOS with real Claude in a dev build: a background server started from a Bash tool call (PPID 1 after its shell exits) was gone within 1 s after closing the chat tab, stopping mid-turn, and quitting the app. An unrelated process stayed alive.

Performance

Stopping a chat now finds the chat's processes before signalling, then rechecks them while they exit, within the existing grace period. Linux walks only the chat's own process tree through each process's children list, never the whole host. macOS reads the process table once per full sweep: the first sweep is immediate, later ones back off from 100 ms to 1 s. Between sweeps it rechecks only the processes it already found. It also reads each candidate orphan's environment at most once per stop. 100 chats stopping at once finish in 50–60 ms. The work runs in each chat's native watcher, not in the app, and idle chats do no extra work.

Risk

Level: Medium.

  • A background process an agent started on purpose (a dev server) now stops with its chat, as it would with a closed terminal.
  • Accepted by the maintainer: on macOS, a process carrying this chat's tag is treated as the chat's even if a same-user launcher handed it the chat's environment. A reviewer's view: a tag shows the process has the chat's environment, not that the chat started it.
  • Accepted by the maintainer: a tool that leaves or joins the CLI's process group in the same instant the watcher is stopping it may get KILL without TERM first, or TERM twice. A reviewer's view: closing that window needs kernel-level tracking, and such a tool could lose work it was flushing.
  • On macOS, a system binary (like bash) whose shell already exited, and which carries no readable tag, is still missed. So is a tool that cleared its environment. Before this PR all of these leaked.
  • On macOS before the audit-token signal API exists, and on Linux without pidfd, detached tools are left alone and the stop reports unverified.
  • Back out with a plain revert.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@brennanb2025
brennanb2025 marked this pull request as ready for review November 24, 2026 06:44
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The native provider watcher now discovers process-tree members on macOS and Linux, signals them using identity checks, and checks tree absence during shutdown. The integration test checks teardown of detached tool processes and preservation of an unrelated process. Ownership recovery no longer attempts to retire an unfinished watcher and keeps its record for a later pass.

Merge Risk: Low · up to ed96b

Closing a chat now stops its background tools, and unrelated processes are left alone. Narrow race windows remain in which a tool may be killed without a TERM first, receive TERM twice, or be missed by the absence check. These are accepted trade-offs with low impact, so the change is mergeable with owner awareness.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Stop a chat's background tools when the chat stops" matches the main change: stopping a chat now also stops the background tool processes it started. The raw summary and objectives describe…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template closely. It includes ELI5, What Changed, Why, Linked Issue (N/A, internal report), Visual Proof (N/A with reason), Testing with checkboxes and platform …
Full details: Title check

Explanation

The title "Stop a chat's background tools when the chat stops" matches the main change: stopping a chat now also stops the background tool processes it started. The raw summary and objectives describe the same process-tree teardown work. The title is specific and concise.

Full details: Description check

Explanation

The description follows the repository template closely. It includes ELI5, What Changed, Why, Linked Issue (N/A, internal report), Visual Proof (N/A with reason), Testing with checkboxes and platform details, Performance, Risk with a Medium level and rollback method, and the checklist. The Linked Issue is marked N/A, which the template does not allow for outside contributors. The author appears to be an internal contributor, so this may be acceptable. The AI Disclosure section is missing, and the Review section is empty. The Testing section names the platforms tested (Linux and macOS) and describes the integration test. The Performance section gives concrete numbers. Overall the description is mostly complete.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a81ac0c0-6cc7-4b96-aa09-6f4c99aa8e22
Commits

Reviewing files that changed from the base of the PR and between 20e2cce and ed96b6e.

Files selected for processing: 1

  • native/provider-exec/provider-exec.c

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0fab89e1-7d9e-4920-8475-8c1845b655ed
Commits

Reviewing files that changed from the base of the PR and between 5384689 and 5c4c759.

Files selected for processing: 4

  • native/provider-exec/provider-exec.c
  • src/main/provider-process/provider-exec-tree-teardown.integration.test.ts
  • src/main/provider-process/provider-group-orphan-guard.ts
  • src/main/provider-process/provider-ownership-journal.ts
Files with no reviewable changes: 1

  • src/main/provider-process/provider-group-orphan-guard.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

Comment thread native/provider-exec/provider-exec.c Resolved
Comment thread native/provider-exec/provider-exec.c Resolved
Comment thread native/provider-exec/provider-exec.c Resolved
Comment thread native/provider-exec/provider-exec.c Resolved
Comment thread native/provider-exec/provider-exec.c Resolved
Comment thread native/provider-exec/provider-exec.c Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 74eae876-7f51-49c7-98e8-4bce41292b00
Commits

Reviewing files that changed from the base of the PR and between 720957a and 20e2cce.

Files selected for processing: 1

  • native/provider-exec/provider-exec.c

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

Comment thread native/provider-exec/provider-exec.c Resolved
@brennanb2025

brennanb2025 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Status on df71c17

Problem: stopping an agent chat on macOS or Linux left the background tools it had started (dev servers, headless browsers, search tools) running with no owner, because they had moved to their own session. Over days they piled up and slowed the machine.

Change: when a chat stops, the native watcher now stops every process in the chat's tree. Each one is signalled through a kernel handle, so a reused PID is never hit. A process it can't prove is the chat's is left alone. Where the tree can't be proven on a host, the stop reports "unverified" instead of "all gone".

Verified

  • macOS with real Claude in a dev build of this head: a background server started from a Bash tool call (orphaned to launchd once its shell exits) was gone within 1.6 s after closing the chat tab, and within 2.5 s after quitting the app. A tagged node tool was stopped, while an untagged process was left running as designed. An unrelated process survived every stop.
  • Linux: the integration test passes, and fails on the previous code. The strict build, typecheck, lint and the changed-file checks all pass.
  • Perf: Linux reads only the chat's own process tree. macOS reads the process table, plus each candidate orphan's environment once per stop. 100 chats stopping at once finish in 50–60 ms. That work runs in the native watchers, not in the app, and idle chats do nothing extra.

Accepted (see Risk): the macOS tag rule, and the sub-millisecond TERM timing when a tool leaves or joins the CLI's process group.

(screenshot removed: contained real product data)
(screenshot removed: contained real product data)

…ons in one pass; follow a traced process's birth parent
…n later is still stopped; batch the untagged cache
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan on df71c17: clean.

  • The watcher does no work while a chat runs: it waits in poll, with zero idle wakeups. All cost falls during a stop, in the native watcher process, never on the app's main or renderer process.
  • Linux reads only the stopped chat's own process tree, through each process's list of children, and never the whole host.
  • macOS has no subreaper, so each stop reads the process table and, once per candidate, the environment of orphaned processes started after the chat. That work is bounded by a per-stop negative cache and a 100 ms to 1 s backoff. At 100 simultaneous stops it comes to about 100k environment reads per burst. Without it these tools are never stopped.
  • Remembered tools are rechecked by their own process handle between full sweeps, so the cost grows with the number of tools, not with the number of processes on the host.

@brennanb2025
brennanb2025 merged commit 8355bb8 into main Nov 25, 2026
63 checks passed
ZinkLu added a commit to ZinkLu/orca that referenced this pull request Nov 26, 2026
Since stablyai#48089 a one-shot's orca-provider-exec stops every process the
command leaves behind, so the daemon `dima runtime list` starts for a
cold listing died with it and every cold DimAgent model listing failed.
That start now runs unsupervised in its own session (on Linux through
orca-provider-exec --exec-without-inherited-fds, like Orca's daemon).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant