Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions lib/internal/streams/iter/broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class BroadcastImpl {

function detach() {
state.detached = true;
state.resolve?.({ __proto__: null, done: true, value: undefined });
state.resolve = null;
state.reject = null;
if (self.#deleteConsumer(state)) {
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/streams/iter/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ class PushQueue {
if (this.#consumerState !== 'active') return;
this.#consumerState = 'returned';
this.#cleanup();
this.#resolvePendingReadsDone();
this.#rejectPendingWrites(
new ERR_INVALID_STATE.TypeError('Stream closed by consumer'));
// If closing, reject the pending end promise
Expand Down Expand Up @@ -526,6 +527,13 @@ class PushQueue {
}
}

#resolvePendingReadsDone() {
while (this.#pendingReads.length > 0) {
this.#pendingReads.shift().resolve(
{ __proto__: null, value: undefined, done: true });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ __proto__: null, value: undefined, done: true });
{ __proto__: null, done: true, value: undefined });

Super minor nit... just keeping the shape the same.

Copy link
Copy Markdown
Member Author

@trivikr trivikr May 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value comes before done in rest of push.js.
And done comes before value in rest of broadcast.js.

I'll make the change in a separate PR after this one is merged. It can happen in parallel.

Copy link
Copy Markdown
Member Author

@trivikr trivikr May 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a specific preference for the order?

In lib/**/*.js source files, excluding tests, docs, and deps:

  • done before value: 30
  • value before done: 20

In Iteration protocols for MDN, done appears before value.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I submitted a PR to enforce done before value in iterator result object in #63526

}
}

#rejectPendingWrites(error) {
while (this.#pendingWrites.length > 0) {
this.#pendingWrites.shift().reject(error);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-stream-iter-broadcast-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ async function testCancelWithReason() {
assert.strictEqual(result.message, 'cancelled');
}

async function testPendingNextSettlesAfterReturn() {
const { broadcast: bc } = broadcast();
const iter = bc.push()[Symbol.asyncIterator]();

const pendingNext = iter.next();
await iter.return();

const result = await pendingNext;
assert.strictEqual(result.done, true);
assert.strictEqual(result.value, undefined);
}

// =============================================================================
// Writer fail detaches consumers
// =============================================================================
Expand Down Expand Up @@ -254,6 +266,7 @@ Promise.all([
testCancelWithoutReason(),
testCancelWithReason(),
testCancelWithFalsyReason(),
testPendingNextSettlesAfterReturn(),
testFailDetachesConsumers(),
testWriterFailIdempotent(),
testLateJoinerSeesBufferedData(),
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-stream-iter-push-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ async function testConsumerBreakWriteSyncReturnsFalse() {
assert.strictEqual(writer.desiredSize, null);
}

async function testPendingNextSettlesAfterReturn() {
const { readable } = push();
const iter = readable[Symbol.asyncIterator]();

const pendingNext = iter.next();
await iter.return();

const result = await pendingNext;
assert.strictEqual(result.done, true);
assert.strictEqual(result.value, undefined);
}

async function testPushWithTransforms() {
const upper = (chunks) => {
if (chunks === null) return null;
Expand Down Expand Up @@ -177,6 +189,7 @@ Promise.all([
testAbortSignal(),
testPreAbortedSignal(),
testConsumerBreakWriteSyncReturnsFalse(),
testPendingNextSettlesAfterReturn(),
testPushWithTransforms(),
testInvalidBackpressure(),
]).then(common.mustCall());
Loading