Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/js/internal/fs/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@
const buf = Buffer.allocUnsafeSlow(n);

this[kFs].read(this.fd, buf, 0, n, this.pos, (er, bytesRead, buf) => {
// If the stream was destroyed while the read was in flight, ignore the
// result. The fd has been (or is about to be) closed by _destroy, and
// pushing more data or EOF here would clobber the destroyed state —
// in particular, push(null) would mark the readable side as ended and
// suppress the ERR_STREAM_PREMATURE_CLOSE that end-of-stream/finished/
// async iteration relies on. Matches Node.js behavior.
if (this.destroyed) {
return;
}
Comment thread
robobun marked this conversation as resolved.

if (er) {
require("internal/streams/destroy").errorOrDestroy(this, er);
} else if (bytesRead > 0) {
Expand All @@ -334,18 +344,13 @@
});
};

readStreamPrototype._destroy = function (this: FSStream, err, cb) {
// Usually for async IO it is safe to close a file descriptor
// even when there are pending operations. However, due to platform
// differences file IO is implemented using synchronous operations
// running in a thread pool. Therefore, file descriptors are not safe
// to close while used in a pending read or write operation. Wait for
// any pending IO (kIsPerformingIO) to complete (kIoDone).
if (this[kReadStreamFastPath]) {
this.once(kReadStreamFastPath, er => close(this, err || er, cb));
} else {
close(this, err, cb);
}
// If a read happens to complete after destroy() has run, the _read
// callback above short-circuits on `this.destroyed`, so the stream's
// readable state can't be clobbered. Close the fd immediately, matching
// the behavior the common `createReadStream(path)` path has had since
// #16754.
close(this, err, cb);

Check warning on line 353 in src/js/internal/fs/streams.ts

View check run for this annotation

Claude / Claude Code Review

kReadStreamFastPath is now write-only dead code

Nit: with the `_destroy` branch removed, `kReadStreamFastPath` is now write-only — the symbol declaration (L43, plus its 4-line comment), the constructor assignment at L209-215, and the commented-out `pipe` stub at L399 are the only remaining references, and none of them read it. Fine to leave as scaffolding for the future pipe fast-path the comment describes, but if that's not on the near-term roadmap you could drop them in this PR while you're here.
Comment thread
robobun marked this conversation as resolved.
};

readStreamPrototype.close = function (cb) {
Expand Down
36 changes: 36 additions & 0 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,42 @@ describe("createReadStream", () => {
},
{ timeout: 100 },
);

// https://github.com/oven-sh/bun/issues/30919
it("async iterator rejects with ERR_STREAM_PREMATURE_CLOSE when destroy() is called during iteration", async () => {
const stream = createReadStream(join(import.meta.dir, "readFileSync.txt"));

let chunks = 0;
let caught: any = undefined;
try {
for await (const _ of stream) {
chunks++;
stream.destroy();
}
} catch (err) {
caught = err;
}

expect(chunks).toBe(1);
expect(caught).toBeDefined();
expect(caught?.code).toBe("ERR_STREAM_PREMATURE_CLOSE");
});

// Regression: _destroy used to register `once(kReadStreamFastPath, ...)` for an
// event that is never emitted when { start, autoClose } were both truthy, so
// 'close' never fired and the fd was leaked.
it("emits 'close' and releases fd with { start: 0, autoClose: true }", async () => {
const stream = createReadStream(join(import.meta.dir, "readFileSync.txt"), { start: 0, autoClose: true });
const { promise, resolve } = Promise.withResolvers<void>();

stream.on("data", () => {});
stream.on("close", () => resolve());

await promise;
expect(stream.destroyed).toBe(true);
expect(stream.closed).toBe(true);
expect(stream.fd).toBeNull();
});
});

describe("fs.WriteStream", () => {
Expand Down
Loading