From 7e6e293045ff39c1ef5b78c11ddf96c589e048af Mon Sep 17 00:00:00 2001 From: Noah Schatz Date: Sat, 28 Nov 2026 16:25:31 -0500 Subject: [PATCH] fix: close the input file when the output pipe closes early --- .changeset/close-input-on-early-stop.md | 5 +++ src/core/input.ts | 23 +++++++--- test/input.test.ts | 57 ++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 .changeset/close-input-on-early-stop.md diff --git a/.changeset/close-input-on-early-stop.md b/.changeset/close-input-on-early-stop.md new file mode 100644 index 0000000..8bfd6d1 --- /dev/null +++ b/.changeset/close-input-on-early-stop.md @@ -0,0 +1,5 @@ +--- +"@cosyte/cli": patch +--- + +`cosyte parse` now closes its input file when the output pipe closes early (a `head` that has seen enough). Before, the file handle was left open, and on Node 26 the garbage collector could turn it into an uncaught error, so the run sometimes exited `1` with a platform error on stderr instead of `74` with `CLI_OUTPUT_WRITE_FAILED`. diff --git a/src/core/input.ts b/src/core/input.ts index 6ddcb41..62549b0 100644 --- a/src/core/input.ts +++ b/src/core/input.ts @@ -234,16 +234,25 @@ async function* readerAsChunks(read: () => Promise): AsyncGenerator< yield* oneChunk(await read()); } -/** Re-attach the chunks detection consumed to the front of the unread remainder. */ +/** + * Re-attach the chunks detection consumed to the front of the unread remainder. A consumer that stops + * early (a write to a closed pipe ends the record loop) finishes this generator, and the `finally` + * finishes the remainder with it, so the file it reads is closed. Left to the garbage collector, the + * open file handle is an uncaught error on Node 26, and the run exited 1 instead of 74. + */ async function* rejoin( head: readonly Uint8Array[], - rest: AsyncIterator, + rest: AsyncGenerator, ): AsyncGenerator { - for (const chunk of head) yield chunk; - for (;;) { - const step = await rest.next(); - if (step.done === true) return; - yield step.value; + try { + for (const chunk of head) yield chunk; + for (;;) { + const step = await rest.next(); + if (step.done === true) return; + yield step.value; + } + } finally { + await rest.return(undefined); } } diff --git a/test/input.test.ts b/test/input.test.ts index 25f7ff4..34477c7 100644 --- a/test/input.test.ts +++ b/test/input.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vitest"; -import { resolveInput } from "../src/core/input.js"; +import { resolveInput, resolveInputStream } from "../src/core/input.js"; import { EXIT } from "../src/core/exit-codes.js"; import { CLI_CODES, CliError } from "../src/core/diagnostics.js"; import type { RunDeps } from "../src/core/io.js"; @@ -88,3 +88,58 @@ describe("resolveInput: the shared input + format front door", () => { await expect(resolveInput("x.json", undefined, boom, "parse")).rejects.toThrow("unexpected"); }); }); + +describe("resolveInputStream releases the input when the consumer stops early", () => { + /** + * A chunk source that records whether it was finished. A real file source closes its file handle + * only when it is finished, and Node 26 turns a file handle left for the garbage collector into an + * uncaught error: a consumer that closed the pipe early then saw exit 1 instead of 74. + */ + function trackedSource(chunks: readonly string[]): { + readonly deps: RunDeps; + readonly finished: () => boolean; + } { + let finished = false; + async function* source(): AsyncGenerator { + try { + for (const c of chunks) yield await Promise.resolve(new TextEncoder().encode(c)); + } finally { + finished = true; + } + } + const deps: RunDeps = { + readFile: () => Promise.reject(new Error("unused")), + readStdin: () => Promise.reject(new Error("unused")), + openFile: () => source(), + }; + return { deps, finished: () => finished }; + } + + it("finishes the source when the consumer stops inside the detection window", async () => { + const t = trackedSource([ + '{"resourceType":"Patient","id":"r0"}\n', + '{"resourceType":"Patient"}\n', + ]); + const r = await resolveInputStream("records.ndjson", "fhir", t.deps, "parse"); + expect(r.ok).toBe(true); + if (!r.ok) return; + const it = r.input.chunks[Symbol.asyncIterator](); + await it.next(); + expect(t.finished()).toBe(false); + await it.return?.(undefined); + expect(t.finished()).toBe(true); + }); + + it("finishes the source when the consumer stops after the detection window", async () => { + const t = trackedSource(["a".repeat(600), "b", "c"]); + const r = await resolveInputStream("records.ndjson", "fhir", t.deps, "parse"); + expect(r.ok).toBe(true); + if (!r.ok) return; + const it = r.input.chunks[Symbol.asyncIterator](); + await it.next(); + await it.next(); + expect(t.finished()).toBe(false); + await it.return?.(undefined); + expect(t.finished()).toBe(true); + }); +});