stream: handle falsy push writer fail reasons

Reject reads based on writer state so explicit falsy fail reasons do
not leave reads pending.

Fixes: https://github.com/nodejs/node/issues/63568

Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Assisted-by: openai:gpt-5.5
PR-URL: https://github.com/nodejs/node/pull/63569
Fixes: https://github.com/nodejs/node/issues/63568
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Trivikram Kamat authored and Antoine du Hamel committed 2026-06-20 17:51:28 +02:00
1 parent b17817eb2b
commit d9967a25b2
2 files changed
+45 -5

No files matched your search

+10 -5
View File
@@ -12,6 +12,7 @@ const {
PromiseReject,
PromiseResolve,
PromiseWithResolvers,
Symbol,
SymbolAsyncDispose,
SymbolAsyncIterator,
SymbolDispose,
@@ -53,6 +54,8 @@ const {
RingBuffer,
} = require('internal/streams/iter/ringbuffer');
const kNoFailReason = Symbol('kNoFailReason');
// =============================================================================
// PushQueue - Internal Queue with Chunk-Based Backpressure
// =============================================================================
@@ -315,14 +318,16 @@ class PushQueue {
* No-op if errored or closed (fully drained).
* If closing (draining), short-circuits the drain.
*/
fail(reason) {
fail(reason = kNoFailReason) {
if (this.#writerState === 'errored' || this.#writerState === 'closed') {
return;
}
const wasClosing = this.#writerState === 'closing';
this.#writerState = 'errored';
this.#error = reason ?? new ERR_INVALID_STATE('Failed');
this.#error = reason === kNoFailReason ?
new ERR_INVALID_STATE('Failed') :
reason;
this.#cleanup();
this.#rejectPendingReads(this.#error);
this.#rejectPendingDrains(this.#error);
@@ -398,7 +403,7 @@ class PushQueue {
return { __proto__: null, done: true, value: undefined };
}
if (this.#writerState === 'errored' && this.#error) {
if (this.#writerState === 'errored') {
throw this.#error;
}
@@ -473,7 +478,7 @@ class PushQueue {
} else if (this.#writerState === 'closed') {
const pending = this.#pendingReads.shift();
pending.resolve({ __proto__: null, done: true, value: undefined });
} else if (this.#writerState === 'errored' && this.#error) {
} else if (this.#writerState === 'errored') {
const pending = this.#pendingReads.shift();
pending.reject(this.#error);
} else if (this.#consumerState === 'returned') {
@@ -624,7 +629,7 @@ class PushWriter {
}
fail(reason) {
this.#queue.fail(reason);
this.#queue.fail(arguments.length === 0 ? kNoFailReason : reason);
}
[SymbolAsyncDispose]() {
@@ -451,6 +451,39 @@ async function testEndRejectsWhenErrored() {
}
}
async function testFailRejectsFutureReadWithFalsyReason() {
for (const reason of [0, null]) {
const { writer, readable } = push();
writer.fail(reason);
const iter = readable[Symbol.asyncIterator]();
await iter.next().then(
common.mustNotCall(),
common.mustCall((rejection) => {
assert.strictEqual(rejection, reason);
}),
);
}
}
async function testFailRejectsPendingReadWithFalsyReason() {
const { writer, readable } = push();
const iter = readable[Symbol.asyncIterator]();
const readPromise = iter.next();
await new Promise(setImmediate);
writer.fail(false);
await readPromise.then(
common.mustNotCall(),
common.mustCall((reason) => {
assert.strictEqual(reason, false);
}),
);
}
Promise.all([
testOndrain(),
testOndrainNonDrainable(),
@@ -473,6 +506,8 @@ Promise.all([
testConsumerThrowRejectsWrites(),
testEndResolvesPendingRead(),
testFailRejectsPendingRead(),
testFailRejectsFutureReadWithFalsyReason(),
testFailRejectsPendingReadWithFalsyReason(),
testConsumerReturnResolvesPendingRead(),
testConsumerThrowRejectsPendingRead(),
testEndRejectsPendingWrites(),