Skip to content

fix(core): pin @opencode/pty 0.6.0 and use protocol 9 - #64251

Merged
jlongster merged 2 commits into
devfrom
pty-reap-on-exit
Nov 17, 2026
Merged

jlongster merged 2 commits into
devfrom
pty-reap-on-exit

Conversation

@jlongster

@jlongster jlongster commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

OpenCode 2.3.0 could retain a zombie for each finished shell command. #64266 fixed the daemon and released @opencode/pty@0.6.0. This PR upgrades the consumers to that published release.

Type of change

  • Bug fix (no feature flag)

What does this PR do?

Pin core, CLI, and server to @opencode/pty@0.6.0. Core expects protocol 9. If a restart hands it a protocol 8 daemon, the existing mismatch check discards the handoff and the next command starts a fresh daemon. After the old server disconnects, its daemon exits on its own: immediately without a handoff, or within two minutes if a prepared handoff goes unclaimed.

The daemon handles failed-command cleanup at exit, so core drops its late SIGTERM. A successful command’s background processes survive when core forgets its finished record.

How did you verify your code works?

  • Core bun run typecheck:tsc passed. The core PTY and shell suites ran with the published binary and a temporary home: 3,268 passed, 340 skipped, 0 failed across 32 files.
  • Core, CLI, and server resolve the published opencode-pty 0.6.0 (protocol 9) binary. The two shell regression tests check that core sends no late signal and the finished command PID is gone.

Screenshots / recordings

Terminal process lifecycle; verification above.

Checklist

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

Added by the opencode bot (AI-generated).

Fixes #63817
Fixes #63976
Fixes #64177

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

The change looks sound, but I couldn't run it. There is no Rust toolchain here, so I couldn't build opencode-pty or compare zombie counts on the base and the PR. In packages/core, the persistent-pty and shell-daemon tests pass except the real-daemon test, which uses the installed protocol-8 npm binary and fails as expected against the v9 client. typecheck:tsc passes. Rust tests and native macOS and Windows runs are left to CI.

Public API: the daemon protocol goes 8 → 9 with the same frame shapes. This is internal between core and opencode-pty and needs a matched release. Behaviour changes on purpose: after exit, signals to a finished command are no-ops, and deleting or evicting a record no longer kills a successful command's background processes. No SDK, HTTP, config or CLI surface changes.

The reaping, failed-group SIGTERM with 3-second SIGKILL, and Windows job disarm logic look right to me. My one concern is the startup sweep that retires protocol-8 daemons (inline). Because the runtime root is shared, it also shuts down the daemon of any older OpenCode server still running.

Fixes #63817
Fixes #63976
Fixes #64177

return yield* attempt.pipe(Effect.catch((error) => (error.kind === "registration" ? attempt : Effect.fail(error))))
})

// v8 keeps finished children as zombies. An owner or an outstanding handoff can keep it alive

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.

root is global.state/pty, which every server shares, and each server picks a fresh crypto.randomUUID() runtime directory. So this loop finds the protocol-8 daemon of any other OpenCode server that is still running, such as a 2.3.x instance in another terminal or a desktop app that bundles an older version. It then shuts that daemon down, and that server loses its running shell commands and persistent terminals. This happens again every time a v9 server starts, and it doesn't stop the zombies there, because the older server just starts a new v8 daemon on its next command.

Could this be limited to the inherited handoff directory, or to v8 daemons that no live server owns? For example, skip a runtime whose owner connection is still held, if the v8 daemon exposes that. The scan also blocks transport creation: each stuck daemon can add two 5-second pings before startup continues.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the protocol 8 sweep in 12ab522. An old daemon already exits on its own: right away when its server exits, or once an unclaimed restart handoff expires (120s). So the new server just sees the protocol mismatch and starts a fresh daemon. That also removes the startup delay.

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 checked 12ab522 and the sweep is fully removed, so this concern is resolved.

  • daemon.ts is back to base apart from the protocol bump. The only remaining change in packages/core/src/persistent-pty/daemon.ts is ProtocolVersion = 9. The readdir scan, the verify and shutdown calls to other runtimes, the SIGTERM and the startup pings are all gone. Other servers' protocol 8 daemons are no longer touched.
  • A protocol 8 handoff is discarded cleanly. discover fails on the protocol check before it pings, so found stays unset and nothing is sent to the old daemon. abandon() (L344–349) then sets id to a new random UUID, so the next command starts a fresh daemon in its own directory. It doesn't hit the old directory and fail with a mismatch again.
  • The adoption-failure behaviour is back to base too. if (found) shutdown and the "stops a discovered inherited daemon when adoption fails" test match pr/base again. That only affects the inherited directory, so it's fine.

Tested: bun test test/persistent-pty-daemon.test.ts passes on a temporary detached worktree of pr/head (11 pass, 0 fail); I removed the worktree afterwards. The shared repo/ checkout is still at 02355c9, which predates your fix, and has an uncommitted bun.lock change I didn't make. I didn't change or reset anything there.

Not tested: that a real protocol 8 daemon exits on its own when its server goes away or its handoff expires after 120s. That depends on the 0.5.x daemon binary, and I didn't run it.

User decisions:
- Restore pre-2.3.0 behavior: reap at exit, kill the group only on failure
- Retire protocol 8 daemons on upgrade; losing their running commands once is accepted
- Pin @opencode/pty 0.6.0 after the daemon-only release
@jlongster jlongster changed the title fix(process-daemon): reap finished shell commands at exit fix(core): pin @opencode/pty 0.6.0 and retire protocol 8 daemons Nov 17, 2026

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

The zombie fix works. I ran opencode serve from source and sent 28 shell commands through /api/shell. On the base (daemon 0.5.2) that left 25 zombies under opencode-pty; with this PR (0.6.0) it left none, and a nohup … & background job stayed alive in both runs. In packages/core, the persistent-pty and shell-daemon tests pass against the published 0.6.0 binary, and typecheck:tsc passes.

Public API: unchanged. The daemon protocol moves 8 → 9 internally, matched by the @opencode/pty 0.5.2 → 0.6.0 pin.

My earlier point is still open: the startup sweep in daemon.ts still shuts down the protocol-8 daemons of other servers that are still running and share the runtime root.

User decisions:
- Remove all protocol 8 retirement code; old daemons exit on their own
@jlongster jlongster changed the title fix(core): pin @opencode/pty 0.6.0 and retire protocol 8 daemons fix(core): pin @opencode/pty 0.6.0 and use protocol 9 Nov 17, 2026
@jlongster
jlongster merged commit a9e910d into dev Nov 17, 2026
24 of 25 checks passed
@jlongster
jlongster deleted the pty-reap-on-exit branch November 17, 2026 02:20

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

My earlier point is addressed: the startup sweep that retired other servers' protocol-8 daemons is gone, and a protocol-8 handoff now goes through the existing mismatch check.

I ran the issue reproductions through opencode serve from source. On the base (daemon 0.5.2), a nohup sleep & job was killed when its command was evicted after 25 later commands, and 3 directories × 30 commands left 25/50/75 zombies. With this PR (0.6.0), the job survived and there were 0 zombies. In packages/core, the persistent-pty, shell-daemon and tool-shell tests pass, and typecheck:tsc passes.

Public API: unchanged. The internal daemon protocol goes 8 → 9, matched by the @opencode/pty 0.5.2 → 0.6.0 pin.

No further concerns.

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

Projects

None yet

1 participant