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: detach shareSync consumers on strict budget errors · nodejs/node@abc4c59

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • stream_iter.md
  • lib/internal/streams/iter
    • share.js
  • test/parallel
    • test-stream-iter-share-sync.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/stream_iter.md‎

Copy file name to clipboardExpand all lines: doc/api/stream_iter.md
+7Lines changed: 7 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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

Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/streams/iter/share.js
+12-2Lines 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) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-share-sync.js
+52Lines 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 (0)
You are visitor 01402775 — 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.