Skip to content

fix: keep only what a plugin command's pipes hold at its exit - #6824

Merged
janicduplessis merged 1 commit into
mainfrom
fix/6816-pipe-drain
Dec 2, 2026
Merged

janicduplessis merged 1 commit into
mainfrom
fix/6816-pipe-drain

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When a cleanupOnExit child's leader exits, reportChild waits up to 1 s for the child's pipes to close, then destroys them (#6818). A process that escaped the group kill (for example a detached: true helper) and keeps the inherited stdout open gets anything it writes in that second appended to the result. A leader that writes {"ok":true} and leaves a helper that writes late afterwards returns {"ok":true}late on main, which breaks callers that parse the whole output as JSON. The wait also costs the full second whenever such a process holds a pipe.

Affected callers: host.exec in project plugins, and spawnManagedCapture().finish(), whose caller has already read the output by then.

Solution

At the leader's observed exit, before the group kill, one nonblocking read takes whatever each output pipe holds, the handle stops reading, and the stream ends the way Node ends it on EOF (onStreamRead). The stream then closes on its own, so the 1 s wait no longer hits its cap on POSIX.

One read, not a loop until EAGAIN: libuv gives child stdio as a Unix stream socketpair with 64 KiB socket buffers, and one read of a stream socket returns what is queued up to the buffer size (1 MiB). A loop would let a detached writer that was blocked on the full pipe refill it after each read. A read that fills the whole 1 MiB may have left output behind (a leader can raise its own SO_SNDBUF), so that stream keeps reading and falls back to the 1 s close-wait, as on main.

Risk: this reads Node's undocumented child.stdout._handle.fd and calls readStop() on the handle. No public API reads a child pipe without waiting for EOF. The issue's other ideas are worse: dropping cleanupOnExit for host.exec would wait for a held pipe until the call's timeout and append even more, and file-backed stdio would still need a size snapshot at the exit.

Limits, also named at the call site:

  • Exit-delivery window. Bytes an escaped process writes between the leader's real exit and Node's exit event are kept. A helper that writes the instant its pipe from the leader closes lands in the output every time (10 of 10 in a local probe). Nothing sharing the pipe can tell those bytes apart, so Leader-exit pipe drain can still truncate or append output in edge cases #6816 stays open for this case.
  • Windows is unchanged and keeps the 1 s close-wait: its pipe handles expose no fd, and the report Job Object stops the tree there.
  • On Linux the kernel can append bytes a woken writer adds while the one read copies, up to the 1 MiB bound; a full read falls back to main's behavior.

The drain runs before cancel stores its promise, so an overflow cancel triggered by the drained bytes takes over cleanup instead of starting a second one.

The issue's truncation case was already fixed by #6818; the split-output row below keeps it covered under the new mechanism.

Test plan

exec.integration.test.ts, on macOS with Node 22.22.2 and 24.22.1:

  • A detached helper waits until the leader has been reaped, then writes late. Fails on main with {"ok":true}late, passes here.
  • A detached helper starts writing 8 MiB, after a stderr handshake, before the leader exits. On main the output grows past 1 MiB; here it stays under. This row fails on main every time; it caught a loop-until-EAGAIN drain only in some runs, since that depends on the writer winning the race.
  • macOS only: a Perl leader raises its SO_SNDBUF to 4 MiB and writes 3 MiB plus a marker. Passes on main, fails with a single read and no fallback, passes here.
  • The split-output row writes 96 KiB from inside the child (an argument that size exceeds Linux and Windows limits), so Node's buffer holds 64 KiB at exit (stdout paused until then) and the pipe the rest. It passes on main too, and fails here if the drained bytes are not pushed into the stream.
  • The existing row for a non-detached holder still returns promptly.

The detached rows are skipped on Windows. PR CI does not run integration tests, so they also ran on Linux through a dispatched CI run on this branch. The host.exec, runCaptured and report-listing suites pass (plugin-command, plugin-output-members, plugin-ios-module, kit-project-worker, engine-deps, report-listing, api-reports), and the full local unit suite passes.

Refs #6816

@janicduplessis janicduplessis added the area: cli packages/stim-cli, including its README and guide label Dec 2, 2026
@janicduplessis

Copy link
Copy Markdown
Collaborator Author

Review outcome for 494c97d.

  • Fresh AI reviewer (round 3, did not implement the change): no blocking findings. It checked normal host.exec output on Node 22.12, 22.22 and 24.22 (small, 5 MiB, 20 MiB, stdout and stderr together, non-zero exit, multibyte UTF-8, git rev-parse), a read that fills the 1 MiB buffer while the stream is flowing (all 3,145,731 bytes arrive), and an overflow cancel set off by the exit read (fails with ENOBUFS, no hang, no second cleanup).
  • Codex adversarial, round 1: the 128 KiB -e fixture exceeded Linux and Windows argument limits, and a read loop chased a detached writer blocked on the full pipe. Both are fixed: the fixture generates its payload in the child, and the drain is one read.
  • Codex adversarial, round 2: a leader that raises its SO_SNDBUF can queue more than one read at exit, and one read then dropped the rest. Fixed: a read that fills the buffer falls back to the close-wait, with a macOS regression.
  • Codex adversarial, round 3: when that fallback runs, a detached writer can still append output within the 1 s close-wait. That is the fallback behaving as main does, so it is not a regression. Under this repository's third-round rule it is recorded on Leader-exit pipe drain can still truncate or append output in edge cases #6816 rather than patched here.

At the leader's observed exit, one nonblocking read takes what each output pipe holds and the stream ends, so a process that escaped the group kill cannot append output while cleanup waits for the pipes to close. A read that fills its 1 MiB buffer falls back to the close-wait. Windows keeps the close-wait. Refs #6816.
@janicduplessis

Copy link
Copy Markdown
Collaborator Author

df9900e has the same tree as the reviewed 494c97d (f7c33d539). It folds the branch into one commit so the squash message says Refs #6816 rather than closing it; #6816 stays open for the cases listed there.

@janicduplessis janicduplessis added the review: ai-approved A fresh AI review found nothing left to fix label Dec 2, 2026
@janicduplessis
janicduplessis marked this pull request as ready for review December 2, 2026 22:02
@janicduplessis
janicduplessis merged commit 9649d7d into main Dec 2, 2026
55 of 56 checks passed
@janicduplessis
janicduplessis deleted the fix/6816-pipe-drain branch December 2, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli packages/stim-cli, including its README and guide review: ai-approved A fresh AI review found nothing left to fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant