Repository navigation
Conversation
…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
There was a problem hiding this comment.
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
echocommands 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/pty0.6.0 daemon, and I couldn't build the Rust daemon here. So I didn't runcargo testorpipe_release.rs, and I couldn't confirm the daemon's own fd and memory release. - Tests:
test/shell-daemon.test.tspasses (19/19). The new cap test fails on the base as expected.tsc -btype-checks cleanly.
Code review
shell.ts: the global eviction loop always ends, becauseremovedrops the shared entry before its early return. Per-Location eviction and restored commands keep the shared list consistent.pipe.rs: the reader only holds aWeakto 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_finishedusestry_lock, so it can't block or deadlock withshutdown. 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
|
@opencodejr thanks for the check of the server side. For the daemon side, the new test @jlongster @neriousy, can you review the |
Issue for this PR
Fixes #67336
Type of change
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:
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_pipeinsrc/service/pipe.rs), andTerminalService::insertjoins 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.Server:
shell.tskeeps 25 exited commands per Location, and the daemonplacesmap 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?
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 ondev(24 fds against 4) and passes with the fix.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 theshell.tschange and passes with it.cargo fmt --check,cargo clippy --all-targets --all-features -- -D warningsandcargo testpass on macOS (arm64).packages/core:bun typecheckpasses, and the shell daemon, shell and shell restart tests pass. One test intool-shell.test.ts("conditional substitutions respect always") fails on my machine ondevtoo, without these changes.Screenshots / recordings
Not a UI change.
Checklist
Added by the opencode bot (AI-generated).
Related: #67349