Skip to content

fix(process-daemon): reap shell commands at exit (protocol 9, release 0.6.0) - #64266

Merged
jlongster merged 2 commits into
devfrom
pty-reap-daemon
Nov 16, 2026
Merged

jlongster merged 2 commits into
devfrom
pty-reap-daemon

Conversation

@jlongster

@jlongster jlongster commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Daemon half of #64251. Jay reported zombie shell commands under opencode-pty on macOS after upgrading to OpenCode 2.3.0. Daemon 0.5.2 deferred waitpid until the command record was forgotten, so each retained record could keep a zombie for days.

Type of change

  • Bug fix (no feature flag)

What does this PR do?

Reap each command at exit and retain its output, metadata and exit status. A successful command’s background processes survive late signals and deletion. On nonzero exit, Unix sends SIGTERM to the group before reaping the leader and escalates surviving members after three seconds; Windows terminates the job immediately. Windows releases the process and job handles at exit. Running commands can still be interrupted or stopped.

The Windows regression test now checks that the PowerShell grandchild survives a late signal and deletion, then terminates it explicitly. The noisy drain test also owns its grandchild’s cleanup if an assertion fails.

Release order: merge this daemon PR → tag process-daemon-v0.6.0 → the release workflow publishes the binaries and npm packages. After publication, #64251 becomes the core half: it pins @opencode/pty@0.6.0 and handles the protocol 8 → 9 upgrade. Core, CLI and server currently pin 0.5.2.

How did you verify your code works?

  • I ran 60 /bin/sh -c "exit 0" commands against each Linux daemon and checked the leaders in /proc while all 60 records were still listed. Published 0.5.2: 60 zombies. This 0.6.0 binary: 0 zombies.
  • On this branch, a frozen Bun install resolved the published 0.5.2 (protocol 8) binary from all three consumers. Core daemon tests: 27 passed. Server persistent-PTY integration: 7 passed.
  • Rust 1.90.0: format and strict Clippy passed on Linux; cargo test --locked passed 147 tests (one ignored subprocess fixture). macOS ARM64 cargo check --tests passed. Windows x64 cargo check --tests and strict test Clippy passed using Ghostty’s -Dsimd=false for this Linux cross-check (no MSVC C++ headers). The local binary reports opencode-pty 0.6.0 (protocol 9).
  • Repository bun run check: 35/35 tasks with the pinned Bun 1.4.4.
  • Native daemon CI: Linux, macOS, Miri and Windows x64 passed, including both PowerShell regression tests. Windows ARM64 failed fetching Ghostty’s Wuffs dependency, before Rust tests. Both Windows release dry-run builds also failed during Ghostty dependency downloads or extraction.
  • Prior ARM64 CI issue: the Windows ARM64 Ghostty build on #64251 hits MSVC STL1000 (the installed headers require Clang 22).

Screenshots / recordings

Process lifecycle change; evidence above.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #63817, #63976, #64177

Public API: opencode-pty protocol 8 → 9 and @opencode/pty 0.5.2 → 0.6.0. Signals to finished commands now do nothing and deleting a finished successful command spares its background processes. Not compatible with protocol 8 clients until core is upgraded (#64251).

@opencodejr opencodejr 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.

I found nothing that blocks a merge. On Linux, the pull request reaps finished commands' leaders and keeps every record.

How I checked

  • I wrote a test that runs 20 finished pipe commands (half exit 0, half exit 3) and counts zombie leaders under /proc. On the base there were 20 zombies and 20 records; with this pull request, 0 zombies and 20 records.
  • With a subreaper (this container's PID 1 doesn't reap orphans), cargo test --test pipe passes 23/23 on both base and head, and the full cargo test passes on head.
  • cargo fmt --check and strict cargo clippy pass.
  • I couldn't run Windows or macOS.

Public API
This changes only the daemon's interface with core, not the SDK, HTTP API, plugins, config or CLI:

  • the protocol goes from 8 to 9 (message shapes unchanged);
  • @opencode/pty goes from 0.5.2 to 0.6.0;
  • signalling a finished command now does nothing and succeeds, where before it reached what was left of its process group;
  • deleting a finished successful command no longer kills its background processes.

Daemon and client must use the same protocol, so 0.6.0 and a protocol 8 client reject each other. Nothing breaks until a consumer pins 0.6.0, and #64251 handles that upgrade.

The 3-second SIGKILL follow-up uses the numeric group ID, the same small reuse risk as the old spawner, which the description already covers. I have no other concerns.

Related: #63817, #63976, #64177

@jlongster
jlongster requested a review from thdxr as a code owner November 16, 2026 19:10
@jlongster
jlongster force-pushed the pty-reap-daemon branch 2 times, most recently from f7442bd to 18c7650 Compare November 16, 2026 21:38
… background processes

User decisions:
- Restore the pre-2.3.0 behavior: background processes a finished command leaves behind are not tracked
- Kill a failed command's process group at exit, on every platform including Windows
- Reap at exit so no zombies or held handles remain
- Bump the daemon protocol so upgrades replace the old daemon
- Release the daemon first, then pin it in core in a separate PR
User decisions:
- Release 0.6.0 for protocol 9
- Pin the published package in core in a separate PR
@jlongster
jlongster enabled auto-merge (squash) November 16, 2026 21:38
@jlongster
jlongster merged commit 03d848f into dev Nov 16, 2026
16 checks passed
@jlongster
jlongster deleted the pty-reap-daemon branch November 16, 2026 21:39
mvanhorn added a commit to mvanhorn/opencode that referenced this pull request Nov 17, 2026
Resolve the conflict with anomalyco#64266 in pipe/windows.rs by keeping both
Job::assign and the new Job::finish.
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