Skip to content

fix(input): close the input file when the output pipe closes early - #138

Merged
NSchatz merged 2 commits into
mainfrom
fix/bun-closed-pipe-exit
Nov 28, 2026
Merged

NSchatz merged 2 commits into
mainfrom
fix/bun-closed-pipe-exit

Conversation

@NSchatz

@NSchatz NSchatz commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

What

rejoin in src/core/input.ts, which puts the detection-window chunks back in front of the rest of the input stream, now finishes that remaining stream in a finally. When cosyte parse stops reading early, because a write to a closed output pipe ended the record loop, the file reader underneath is finished too and closes its file handle.

Two cases in test/input.test.ts stop a resolveInputStream stream early (inside the detection window, and after it) and check that the chunk source was finished. A patch changeset.

Why

When the consumer stopped early, the generator returned without finishing the stream it wrapped, so fileChunks never reached the finally that closes its FileHandle. Node 26 turns a file handle closed by the garbage collector into an uncaught ERR_INVALID_STATE, so a run whose output pipe closed sometimes exited 1 with a platform error on stderr instead of 74 with CLI_OUTPUT_WRITE_FAILED. It showed in ci / bun (where the test's spawned bins ran on Node 26) as intermittent pipe-close failures. It is not Bun-specific and not in src/bin/.

Proof

  • Reproduced before the fix: spawning the bin through tsx on two pinned cores (taskset -c 0,1), 75 runs with stdout closed at once: one exited 1 with A FileHandle object was closed during garbage collection ... ERR_INVALID_STATE. After the fix, 75 of 75 exited 74.
  • The two new cases fail without the change (expected false to be true, the source was never finished) and pass with it, under Node and under Bun (bun --bun x vitest run test/input.test.ts: 12 passed).
  • pnpm test:coverage: 823 passed, 100% on every metric. pnpm quality:check --base origin/main: OK.
  • pnpm mutate changed --base origin/main then pnpm quality:mutation --base origin/main: OK, no surviving mutant on a changed line.
  • pnpm typecheck, pnpm lint, pnpm format:check, pnpm phi-scan, pnpm check:no-emdash, pnpm check:no-internal-refs: OK.
  • No output path changed: the fix closes a file and writes nothing.

Choices

  • A unit test on the stream rather than a spawned-process test: the process failure depends on when the garbage collector runs, so it reproduces only some of the time, while the unclosed source is visible every time.
  • The branch name says Bun because that is where the failure first showed; the cause is general.

@NSchatz
NSchatz merged commit 074af55 into main Nov 28, 2026
26 checks passed
@NSchatz
NSchatz deleted the fix/bun-closed-pipe-exit branch November 28, 2026 21:38
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