GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: more iter streams fixes by jasnell · Pull Request #66030 · nodejs/node

stream: more iter streams fixes #66030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jasnell wants to merge 7 commits into nodejs:main from jasnell:jasnell/moar-iter-streams-2

Uh oh!

There was an error while loading. Please reload this page.

Closed

stream: more iter streams fixes #66030

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
37bec3d
stream: ensure nested async iterators are cancellation aware
jasnell Aug 30, 2026
4acd6f1
stream: make broadcast waiters use SafeSet
jasnell Aug 30, 2026
00f4652
stream: ensure protocol and transform accessors are called once
jasnell Aug 30, 2026
8cc140e
stream: have broadcast clean factory listeners on cancel
jasnell Aug 30, 2026
efc2f37
stream: ensure streamable protocols precede array/validated-source fa…
jasnell Aug 30, 2026
a409c52
stream: make Broadcast.from() only return the Broadcast
jasnell Aug 30, 2026
d0ff99c
stream: update ShareImpl to retain factory abort listener
jasnell Sep 17, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
stream: make broadcast waiters use SafeSet
Signed-off-by: James M Snell <[email protected]>
Assisted-by: Opencode
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
jasnell committed Sep 17, 2026
commit 4acd6f1499f2bf0121bca665fe8687da16fcbdb5

There are no files selected for viewing

19 changes: 11 additions & 8 deletions 19 lib/internal/streams/iter/broadcast.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class BroadcastImpl {
#buffer = new RingBuffer();
#bufferStart = 0;
#consumers = new SafeSet();
#waiters = []; // Consumers with pending resolve (subset of #consumers)
#waiters = new SafeSet(); // Consumers with pending resolve
#ended = false;
#error;
#errored = false;
Expand Down Expand Up @@ -203,6 +203,7 @@ class BroadcastImpl {

function detach() {
state.detached = true;
self.#waiters.delete(state);
if (state.resolve) {
state.resolve({ __proto__: null, done: true, value: undefined });
}
Expand Down Expand Up @@ -262,7 +263,7 @@ class BroadcastImpl {
const { promise, resolve, reject } = PromiseWithResolvers();
state.resolve = resolve;
state.reject = reject;
ArrayPrototypePush(self.#waiters, state);
self.#waiters.add(state);
return promise;
},

Expand Down Expand Up @@ -313,6 +314,7 @@ class BroadcastImpl {
consumer.detached = true;
}
this.#consumers.clear();
this.#waiters.clear();
this.#cachedMinCursorConsumers = 0;
const onCancel = this[kOnCancel];
this[kOnCancel] = null;
Expand Down Expand Up @@ -401,6 +403,7 @@ class BroadcastImpl {
}
}
}
this.#waiters.clear();
this.#notifyEndDrained();
}

Expand All @@ -422,6 +425,7 @@ class BroadcastImpl {
consumer.detached = true;
}
this.#consumers.clear();
this.#waiters.clear();
this.#cachedMinCursorConsumers = 0;
}

Expand Down Expand Up @@ -489,12 +493,11 @@ class BroadcastImpl {

#notifyConsumers() {
const waiters = this.#waiters;
if (waiters.length === 0) return;
if (waiters.size === 0) return;
// Swap out the waiters list so consumers that re-wait during
// resolve don't get processed twice in this cycle.
this.#waiters = [];
for (let i = 0; i < waiters.length; i++) {
const consumer = waiters[i];
this.#waiters = new SafeSet();
for (const consumer of waiters) {
if (consumer.resolve) {
const bufferIndex = consumer.cursor - this.#bufferStart;
if (bufferIndex < this.#buffer.length) {
Expand All @@ -513,11 +516,11 @@ class BroadcastImpl {
if (consumer.detached && this.#deleteConsumer(consumer)) {
this.#tryTrimBuffer();
} else if (this.#promotePending(consumer)) {
ArrayPrototypePush(this.#waiters, consumer);
this.#waiters.add(consumer);
}
} else {
// Still waiting -- put back
ArrayPrototypePush(this.#waiters, consumer);
this.#waiters.add(consumer);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions 34 test/parallel/test-stream-iter-broadcast-waiter-cleanup.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Flags: --experimental-stream-iter --expose-gc
'use strict';

const common = require('../common');
const assert = require('assert');
const { broadcast } = require('stream/iter');

async function detachConsumers(shared, count) {
for (let i = 0; i < count; i++) {
const iterator = shared.push()[Symbol.asyncIterator]();
const pending = iterator.next();
await iterator.return();
await pending;
}
}

async function testDetachedWaitersAreReleased() {
const { broadcast: shared } = broadcast();

await detachConsumers(shared, 100);
global.gc();
const before = process.memoryUsage().heapUsed;

await detachConsumers(shared, 50_000);
global.gc();
const retained = process.memoryUsage().heapUsed - before;

assert.ok(retained < 8 * 1024 * 1024,
`Detached Broadcast waiters retained ${retained} bytes`);
assert.strictEqual(shared.consumerCount, 0);
shared.cancel();
}

testDetachedWaitersAreReleased().then(common.mustCall());
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.