mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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 Backport-PR-URL: https://github.com/nodejs/node/pull/64675 Fixes: https://github.com/nodejs/node/issues/63568 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
3b8c30c096
commit
4ba1fa0062
2 files changed
+45
-5
No files matched your search
@@ -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, value: undefined, done: true };
|
||||
}
|
||||
|
||||
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, value: undefined, done: true });
|
||||
} 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]() {
|
||||
|
||||
@@ -454,6 +454,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(),
|
||||
@@ -476,6 +509,8 @@ Promise.all([
|
||||
testConsumerThrowRejectsWrites(),
|
||||
testEndResolvesPendingRead(),
|
||||
testFailRejectsPendingRead(),
|
||||
testFailRejectsFutureReadWithFalsyReason(),
|
||||
testFailRejectsPendingReadWithFalsyReason(),
|
||||
testConsumerReturnResolvesPendingRead(),
|
||||
testConsumerThrowRejectsPendingRead(),
|
||||
testEndRejectsPendingWrites(),
|
||||
|
||||
Reference in new issue
Block a user