Skip to content

fix(process-daemon): stop leaking pipe fds and records of exited shell commands - #67363

Open
GuitarWag wants to merge 2 commits into
anomalyco:devfrom
GuitarWag:fix/pty-exited-pipe-leak
Open

GuitarWag wants to merge 2 commits into
anomalyco:devfrom
GuitarWag:fix/pty-exited-pipe-leak

Conversation

@GuitarWag

@GuitarWag GuitarWag commented Nov 30, 2026 •

Copy link
Copy Markdown

Issue for this PR

Fixes #67336

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

With a script that runs many opencode agents in parallel (about 27, one opencode serve --service), the opencode-pty 0.6.0 daemon held 3177 pipe fds and 1.1 GB after 28 hours, with no child processes. After a restart it grew by about 60 pipes per hour.

Two causes:

  1. Daemon: an exited pipe process keeps its record until a client sends terminate, which is intended. But the record also kept the cancel pipe's write end open (one fd each) and the finished reader and waiter threads unjoined. Now the reader closes the cancel handle when it ends (start_pipe in src/service/pipe.rs), and TerminalService::insert joins worker threads that already ended. In a local run of 1000 short commands, resident memory per kept record went from about 160 KB to about 53 KB, and the fd count no longer grows.

  2. Server: shell.ts keeps 25 exited commands per Location, and the daemon places map never drops a Location. With one worktree per agent, the server kept up to 25 daemon records for every directory it ever saw, so the count had no bound. Exited daemon commands are now also capped at 100 across the server, oldest first. The per-Location cap of 25 stays.

How did you verify your code works?

  • New packages/process-daemon/tests/pipe_release.rs: runs 20 short pipe commands and checks the process's open fds return to the start value. It fails on dev (24 fds against 4) and passes with the fix.
  • New test in packages/core/test/shell-daemon.test.ts: 5 Locations finish 21 commands each, and the fake daemon must keep only the latest 100. It fails without the shell.ts change and passes with it.
  • cargo fmt --check, cargo clippy --all-targets --all-features -- -D warnings and cargo test pass on macOS (arm64).
  • In packages/core: bun typecheck passes, and the shell daemon, shell and shell restart tests pass. One test in tool-shell.test.ts ("conditional substitutions respect always") fails on my machine on dev too, without these changes.

Screenshots / recordings

Not a UI change.

Checklist

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

Added by the opencode bot (AI-generated).

Related: #67349

…eads

An exited pipe process keeps its record until a client terminates it, but
the record also kept the reader's cancel pipe open and the finished reader
and waiter threads unjoined. The reader now closes the cancel handle when it
ends, and each create joins worker threads that already ended.

Refs anomalyco#67336
Each Location kept its 25 latest exited commands in the process daemon, and
a server never drops a Location's entry, so a server that sees many
directories (one worktree per agent) kept daemon records without bound.
Exited daemon commands are now also capped at 100 across the server.

Fixes anomalyco#67336

@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. I checked the server-side cap end to end. I couldn't build the daemon, so its half is reviewed from the code only.

What I ran

  • Base: the #67336 reproduction from source (6 directories, 40 echo commands each). The daemon's pipe count grew by 25 per directory, to 150 pipes and 134 MB, and nothing was released when idle.
  • This PR: the count stops at 100 pipes and about 93 MB once the new cap of 100 is reached, from the fifth directory on.
  • Daemon fix: running from source uses the prebuilt @opencode/pty 0.6.0 daemon, and I couldn't build the Rust daemon here. So I didn't run cargo test or pipe_release.rs, and I couldn't confirm the daemon's own fd and memory release.
  • Tests: test/shell-daemon.test.ts passes (19/19). The new cap test fails on the base as expected. tsc -b type-checks cleanly.

Code review

  • shell.ts: the global eviction loop always ends, because remove drops the shared entry before its early return. Per-Location eviction and restored commands keep the shared list consistent.
  • pipe.rs: the reader only holds a Weak to the cancel handle. Stopping a running process still closes the handle right away, and a finished reader frees it itself. This also holds for the Windows event handle.
  • service.rs: join_finished uses try_lock, so it can't block or deadlock with shutdown. Joining threads that have already finished doesn't wait.

The daemon half only reaches users once a new @opencode/pty is published and the 0.6.0 dependency is bumped. This PR doesn't do that, which is presumably handled separately.

Public API: unchanged. No SDK, HTTP, config, CLI or plugin surface is touched, and the daemon protocol is the same.

Related: #67349

@GuitarWag

Copy link
Copy Markdown
Author

@opencodejr thanks for the check of the server side. For the daemon side, the new test packages/process-daemon/tests/pipe_release.rs runs 20 short pipe commands. Then it checks that the open fds go back to the start value. On dev the test fails with 24 fds against 4. With this PR it passes. To run it: cargo test --test pipe_release in packages/process-daemon. The build needs zig 0.16.0 on PATH, the same as CI.

@jlongster @neriousy, can you review the process-daemon part? @kitlangton, can you review the shell.ts limit (DAEMON_EXITED_LIMIT = 100)?

This branch has not been deployed

No deployments
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.

opencode-pty daemon leaks pipe fds and memory after shell commands exit (macOS, 0.6.0)

1 participant