diff --git a/doc/api/stream_iter.md b/doc/api/stream_iter.md index 2ca44ecbe56..94b50135ee2 100644 --- a/doc/api/stream_iter.md +++ b/doc/api/stream_iter.md @@ -1441,13 +1441,21 @@ added: v24.20.0 * `source` {Iterable} The sync source to share. * `options` {Object} - * `highWaterMark` {number} Must be >= 1; values below 1 are clamped - to 1. **Default:** `16`. - * `backpressure` {string} **Default:** `'strict'`. + * `budget` {number} Must be >= 16384. + **Default:** `65536`. + * `backpressure` {string} `'strict'`, `'drop-oldest'`, or `'drop-newest'`. + **Default:** `'strict'`. * Returns: {SyncShare} Synchronous version of [`share()`][]. +Because there is no way to wait in a synchronous context, `'unbounded'` is not +supported and throws `ERR_INVALID_ARG_VALUE`. With `'drop-newest'`, a consumer +that reaches the end of the buffer while the budget is exhausted discards a +single entry from the source and then returns `{ done: true }` without a +value; the consumer is not detached, so it can resume once the slowest +consumer advances and releases budget. + ### Class: `SyncShare` #### Static method: `SyncShare.fromSync(input[, options])` diff --git a/lib/internal/streams/iter/share.js b/lib/internal/streams/iter/share.js index a678299b45e..4c09470b31f 100644 --- a/lib/internal/streams/iter/share.js +++ b/lib/internal/streams/iter/share.js @@ -576,6 +576,7 @@ class SyncShareImpl { } // Check buffer limit + let dropped = false; if (self.#bufferedBytes >= self.#options.budget) { switch (self.#options.backpressure) { case 'strict': @@ -598,17 +599,20 @@ class SyncShareImpl { self.#recomputeMinCursor(); break; case 'drop-newest': - while (self.#bufferedBytes >= self.#options.budget && - !self.#sourceExhausted && - !self.#cancelled && - self.#sourceError === kNoShareError) { - self.#pullFromSource(true); - } + // Discarding does not reclaim budget, and the slowest + // consumer cannot advance while this synchronous next() is + // running, so at most one entry may be dropped per call. + // Looping here would spin forever on an unbounded source + // and drain a finite one in a single call. + self.#pullFromSource(true); + dropped = true; break; } } - self.#pullFromSource(); + if (!dropped) { + self.#pullFromSource(); + } if (self.#sourceError !== undefined) { state.detached = true; diff --git a/test/parallel/test-stream-iter-share-sync.js b/test/parallel/test-stream-iter-share-sync.js index 7339196f4dc..6c79a03633a 100644 --- a/test/parallel/test-stream-iter-share-sync.js +++ b/test/parallel/test-stream-iter-share-sync.js @@ -149,7 +149,7 @@ function testShareSyncRejectsUnbounded() { function testShareSyncDropNewest() { let pulls = 0; function* source() { - for (let i = 0; i < 3; i++) { + for (let i = 0; i < 4; i++) { pulls++; const chunk = new Uint8Array(16384); chunk[0] = i; @@ -165,11 +165,50 @@ function testShareSyncDropNewest() { const slow = shared.pull()[Symbol.iterator](); assert.strictEqual(fast.next().value[0][0], 0); - assert.strictEqual(fast.next().done, true); - assert.strictEqual(pulls, 3); + // The budget is exhausted and the slow consumer cannot advance while this + // call is running, so exactly one entry is dropped and no value is + // available. The consumer is not detached. + assert.strictEqual(fast.next().done, true); + assert.strictEqual(pulls, 2); + + // The slow consumer still sees the buffered entry, which releases budget. assert.strictEqual(slow.next().value[0][0], 0); - assert.strictEqual(slow.next().done, true); + + // Entry 1 was dropped for every consumer, so both resume at entry 2. + assert.strictEqual(slow.next().value[0][0], 2); + assert.strictEqual(pulls, 3); + assert.strictEqual(fast.next().value[0][0], 2); +} + +// Regression test: a full buffer must not spin pulling-and-discarding from an +// unbounded source, since discarding never reclaims budget. +function testShareSyncDropNewestUnboundedSource() { + let pulls = 0; + function* source() { + for (;;) { + pulls++; + yield [new Uint8Array(16384)]; + } + } + + const shared = shareSync(source(), { + budget: 16384, + backpressure: 'drop-newest', + }); + const fast = shared.pull()[Symbol.iterator](); + shared.pull(); + + assert.strictEqual(fast.next().done, false); + assert.strictEqual(pulls, 1); + + // Each blocked call drops at most one entry and returns without a value. + for (let i = 0; i < 3; i++) { + assert.strictEqual(fast.next().done, true); + assert.strictEqual(pulls, 2 + i); + } + + shared.cancel(); } // shareSync() accepts string source directly (normalized via fromSync()) @@ -209,6 +248,7 @@ Promise.all([ testShareSyncSourceError(), testShareSyncRejectsUnbounded(), testShareSyncDropNewest(), + testShareSyncDropNewestUnboundedSource(), testShareSyncStringSource(), testShareSyncRetainsBufferWhenAllConsumersDetach(), ]).then(common.mustCall());