stream: keep share buffer when all consumers detach

When the last consumer of a share() or shareSync() detached, the min
cursor fell back to the end of the buffer, so every buffered entry was
trimmed, including entries that the detaching consumer had not read.
The source had already produced that data, so consumers that attached
later silently skipped it.

Keep the buffer while there are no consumers, as broadcast() already
does, so late-joining consumers start at the oldest entry still in the
buffer. Document that the source stays open until the share is
cancelled or disposed.

Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66483
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
James M Snell authored and Richard Lau committed 2026-10-12 18:04:41 +00:00
1 parent 69148ee0ff
commit fa49de9ba6
4 files changed
+59

No files matched your search

+6
View File
@@ -1440,6 +1440,12 @@ Create a pull-model multi-consumer shared stream. Unlike `broadcast()`, the
source is only read when a consumer pulls. Multiple consumers share a single
buffer.
A consumer created with `share.pull()` starts reading at the oldest entry still
in the buffer. Entries are released once every consumer has read them. When
every consumer has detached, the buffered data is kept for consumers that
attach later, and the source is not closed. Call `share.cancel()` (or dispose
the share) to release the source once it is no longer needed.
```mjs
import { from, share, text } from 'node:stream/iter';
+10
View File
@@ -448,6 +448,11 @@ class ShareImpl {
}
#tryTrimBuffer() {
// Retain buffered data for consumers that attach while none are active.
// Without this, the last consumer detaching would discard entries that
// a late-joining consumer has not read and cannot get back from the
// source.
if (this.#consumers.size === 0) return;
if (this.#cachedMinCursorConsumers === 0) {
this.#recomputeMinCursor();
}
@@ -744,6 +749,11 @@ class SyncShareImpl {
}
#tryTrimBuffer() {
// Retain buffered data for consumers that attach while none are active.
// Without this, the last consumer detaching would discard entries that
// a late-joining consumer has not read and cannot get back from the
// source.
if (this.#consumers.size === 0) return;
if (this.#cachedMinCursorConsumers === 0) {
this.#recomputeMinCursor();
}
@@ -379,6 +379,27 @@ async function testShareLateJoiningConsumer() {
assert.strictEqual(data2, '');
}
async function testShareRetainsBufferWhenAllConsumersDetach() {
// Data that a consumer had not read yet must stay available to consumers
// that attach after every previous consumer has detached.
const enc = new TextEncoder();
async function* gen() {
yield [enc.encode('a')];
yield [enc.encode('b')];
yield [enc.encode('c')];
}
const shared = share(gen(), { budget: 16384 });
const c1 = shared.pull()[Symbol.asyncIterator]();
const c2 = shared.pull()[Symbol.asyncIterator]();
assert.deepStrictEqual((await c1.next()).value, [enc.encode('a')]);
// c2 has not read 'a' yet; it is the last consumer to detach.
await c1.return();
await c2.return();
assert.strictEqual(shared.consumerCount, 0);
assert.strictEqual(await text(shared.pull()), 'abc');
}
async function testShareConsumerBreak() {
// Verify that a consumer breaking mid-iteration detaches properly
const enc = new TextEncoder();
@@ -473,6 +494,7 @@ Promise.all([
testShareSourceError(),
testShareSourceErrorFollowsBufferedData(),
testShareLateJoiningConsumer(),
testShareRetainsBufferWhenAllConsumersDetach(),
testShareConsumerBreak(),
testShareMultipleConsumersConcurrentPull(),
testShareConsumerConcurrentNextCalls(),
@@ -212,6 +212,26 @@ function testShareSyncDropNewestUnboundedSource() {
}
// shareSync() accepts string source directly (normalized via fromSync())
function testShareSyncRetainsBufferWhenAllConsumersDetach() {
// Data that a consumer had not read yet must stay available to consumers
// that attach after every previous consumer has detached.
const enc = new TextEncoder();
function* gen() {
yield [enc.encode('a')];
yield [enc.encode('b')];
yield [enc.encode('c')];
}
const shared = shareSync(gen(), { budget: 16384 });
const c1 = shared.pull()[Symbol.iterator]();
const c2 = shared.pull()[Symbol.iterator]();
assert.deepStrictEqual(c1.next().value, [enc.encode('a')]);
c1.return();
c2.return();
assert.strictEqual(shared.consumerCount, 0);
assert.strictEqual(textSync(shared.pull()), 'abc');
}
function testShareSyncStringSource() {
const shared = shareSync('hello-sync-share');
const result = textSync(shared.pull());
@@ -230,4 +250,5 @@ Promise.all([
testShareSyncDropNewest(),
testShareSyncDropNewestUnboundedSource(),
testShareSyncStringSource(),
testShareSyncRetainsBufferWhenAllConsumersDetach(),
]).then(common.mustCall());