Skip to content

Commit abc4c59

Browse files
jasnelladuh95
authored andcommitted
stream: detach shareSync consumers on strict budget errors
When a shareSync() consumer needed to pull while the buffer was at or above the budget under the 'strict' policy, it threw ERR_OUT_OF_RANGE but stayed registered. Neither for...of nor a pullSync() transform pipeline calls return() when next() throws, so the abandoned consumer kept its cursor forever, pinned every entry pulled afterwards, and made the remaining consumers fail with ERR_OUT_OF_RANGE as well. Detach the consumer before throwing, as the async share() already does (see 1ad67bc), and document the behavior for both. The spec notes that a rejected strict pull does not terminate the consumer's iterator so that it may be retried. That is not safe with the common iteration patterns, and will be raised with the spec editors. Assisted-by: OpenCode Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #66483 Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 87d619f commit abc4c59

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

‎doc/api/stream_iter.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,13 @@ every consumer has detached, the buffered data is kept for consumers that
13461346
attach later, and the source is not closed. Call `share.cancel()` (or dispose
13471347
the share) to release the source once it is no longer needed.
13481348

1349+
With `'strict'` backpressure, a consumer that needs to pull from the source
1350+
while the buffer is at or above `budget` is rejected with `ERR_OUT_OF_RANGE`
1351+
and detached; further reads from that consumer complete with `{ done: true }`.
1352+
Detaching keeps a consumer that is not retried (for example, one read with
1353+
`for await...of`, which does not call `return()` when a read rejects) from
1354+
holding buffered data and blocking the other consumers.
1355+
13491356
```mjs
13501357
import { from, share, text } from 'node:stream/iter';
13511358

‎lib/internal/streams/iter/share.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,20 @@ class SyncShareImpl {
601601
let dropped = false;
602602
if (self.#bufferedBytes >= self.#options.budget) {
603603
switch (self.#options.backpressure) {
604-
case 'strict':
605-
throw new ERR_OUT_OF_RANGE(
604+
case 'strict': {
605+
const error = new ERR_OUT_OF_RANGE(
606606
'buffered bytes', `< ${self.#options.budget}`,
607607
self.#bufferedBytes);
608+
// Detach before throwing, as the async share does. Neither
609+
// for...of nor a transform pipeline calls return() when
610+
// next() throws, so a consumer left registered here would
611+
// keep its cursor forever and wedge the other consumers.
612+
state.detached = true;
613+
if (self.#deleteConsumer(state)) {
614+
self.#tryTrimBuffer();
615+
}
616+
throw error;
617+
}
608618
case 'drop-oldest':
609619
while (self.#bufferedBytes >= self.#options.budget &&
610620
self.#buffer.length > 0) {

‎test/parallel/test-stream-iter-share-sync.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,56 @@ function testShareSyncRetainsBufferWhenAllConsumersDetach() {
232232
assert.strictEqual(textSync(shared.pull()), 'abc');
233233
}
234234

235+
function testShareSyncStrictBackpressureDetaches() {
236+
for (const transformed of [false, true]) {
237+
function* source() {
238+
for (let i = 0; i < 10; i++) {
239+
yield [new Uint8Array(16384)];
240+
}
241+
}
242+
const shared = shareSync(source(), {
243+
budget: 32768,
244+
backpressure: 'strict',
245+
});
246+
const consumer = transformed ?
247+
shared.pull((chunks) => chunks) : shared.pull();
248+
const fast = consumer[Symbol.iterator]();
249+
// This consumer prevents the buffer from being trimmed.
250+
const slow = shared.pull()[Symbol.iterator]();
251+
252+
fast.next();
253+
fast.next();
254+
assert.throws(() => fast.next(), { code: 'ERR_OUT_OF_RANGE' });
255+
// The rejected consumer is detached, as with the async share.
256+
assert.strictEqual(shared.consumerCount, 1);
257+
assert.strictEqual(fast.next().done, true);
258+
259+
// The detached consumer no longer pins the buffer, so the remaining
260+
// consumer can read the whole source.
261+
let count = 0;
262+
while (!slow.next().done) count++;
263+
assert.strictEqual(count, 10);
264+
}
265+
}
266+
267+
function testShareSyncStrictForOfDoesNotWedgeOthers() {
268+
// for...of does not call return() when next() throws. The consumer that
269+
// hit the budget must still not keep the other consumers from reading.
270+
function* source() {
271+
for (let i = 0; i < 20; i++) yield [new Uint8Array(8192)];
272+
}
273+
const shared = shareSync(source(), { budget: 16384, backpressure: 'strict' });
274+
const slow = shared.pull()[Symbol.iterator]();
275+
assert.throws(() => {
276+
// eslint-disable-next-line no-unused-vars
277+
for (const _ of shared.pull()) { /* consume */ }
278+
}, { code: 'ERR_OUT_OF_RANGE' });
279+
assert.strictEqual(shared.consumerCount, 1);
280+
let count = 0;
281+
while (!slow.next().done) count++;
282+
assert.strictEqual(count, 20);
283+
}
284+
235285
function testShareSyncStringSource() {
236286
const shared = shareSync('hello-sync-share');
237287
const result = textSync(shared.pull());
@@ -251,4 +301,6 @@ Promise.all([
251301
testShareSyncDropNewestUnboundedSource(),
252302
testShareSyncStringSource(),
253303
testShareSyncRetainsBufferWhenAllConsumersDetach(),
304+
testShareSyncStrictBackpressureDetaches(),
305+
testShareSyncStrictForOfDoesNotWedgeOthers(),
254306
]).then(common.mustCall());

0 commit comments

Comments
 (0)