mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
stream: prevent share from eagerly draining source
Wait for buffer space after drop-newest discards an upstream result. This keeps one consumer pull from draining the source or looping indefinitely while a slower consumer keeps the buffer full. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: codex:gpt-5.6-sol PR-URL: https://github.com/nodejs/node/pull/65338 Fixes: https://github.com/nodejs/node/issues/65337 Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
057e591f7c
commit
f96dccc271
2 files changed
+50
-26
No files matched your search
@@ -199,6 +199,9 @@ class ShareImpl {
|
||||
}
|
||||
|
||||
await self.#pullFromSource(!shouldBuffer);
|
||||
if (!shouldBuffer) {
|
||||
await self.#waitForBufferSpaceAfterDrop();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -317,6 +320,17 @@ class ShareImpl {
|
||||
return true;
|
||||
}
|
||||
|
||||
async #waitForBufferSpaceAfterDrop() {
|
||||
while (this.#bufferedBytes >= this.#options.budget &&
|
||||
!this.#cancelled &&
|
||||
this.#sourceError === undefined &&
|
||||
!this.#sourceExhausted) {
|
||||
const { promise, resolve } = PromiseWithResolvers();
|
||||
ArrayPrototypePush(this.#pullWaiters, resolve);
|
||||
await promise;
|
||||
}
|
||||
}
|
||||
|
||||
#pullFromSource(discard = false) {
|
||||
if (this.#sourceExhausted || this.#cancelled) {
|
||||
return PromiseResolve();
|
||||
|
||||
@@ -170,40 +170,50 @@ async function testShareDropOldest() {
|
||||
}
|
||||
|
||||
async function testShareDropNewest() {
|
||||
// With drop-newest and a stalled consumer, the async path allows the
|
||||
// buffer to grow beyond budget (the "drop" applies to the
|
||||
// backpressure signal, not the buffer contents). Both consumers
|
||||
// ultimately see all items.
|
||||
let pulls = 0;
|
||||
let secondPull;
|
||||
const secondPullStarted = new Promise((resolve) => {
|
||||
secondPull = resolve;
|
||||
});
|
||||
|
||||
async function* source() {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
for (let i = 0; i < 7; i++) {
|
||||
pulls++;
|
||||
if (pulls === 2) secondPull();
|
||||
const chunk = new Uint8Array(16384);
|
||||
chunk[0] = i;
|
||||
yield [chunk];
|
||||
}
|
||||
}
|
||||
const shared = share(source(), { budget: 32768, backpressure: 'drop-newest' });
|
||||
const fast = shared.pull();
|
||||
const slow = shared.pull();
|
||||
const shared = share(source(), {
|
||||
budget: 16384,
|
||||
backpressure: 'drop-newest',
|
||||
});
|
||||
const fast = shared.pull()[Symbol.asyncIterator]();
|
||||
const slow = shared.pull()[Symbol.asyncIterator]();
|
||||
|
||||
// Fast consumer reads all items
|
||||
const fastIndices = [];
|
||||
for await (const batch of fast) {
|
||||
for (const chunk of batch) {
|
||||
fastIndices.push(chunk[0]);
|
||||
}
|
||||
}
|
||||
assert.strictEqual(fastIndices.length, 2);
|
||||
const first = await fast.next();
|
||||
assert.strictEqual(first.value[0][0], 0);
|
||||
|
||||
// Slow consumer also sees all items (buffer grew past budget)
|
||||
const slowIndices = [];
|
||||
for await (const batch of slow) {
|
||||
for (const chunk of batch) {
|
||||
slowIndices.push(chunk[0]);
|
||||
}
|
||||
}
|
||||
assert.strictEqual(slowIndices.length, 2);
|
||||
assert.strictEqual(slowIndices[0], 0);
|
||||
assert.strictEqual(slowIndices[1], 1);
|
||||
let nextSettled = false;
|
||||
const next = fast.next().then((result) => {
|
||||
nextSettled = true;
|
||||
return result;
|
||||
});
|
||||
|
||||
await secondPullStarted;
|
||||
await new Promise(setImmediate);
|
||||
assert.strictEqual(pulls, 2);
|
||||
assert.strictEqual(nextSettled, false);
|
||||
|
||||
const slowResult = await slow.next();
|
||||
assert.strictEqual(slowResult.value[0][0], 0);
|
||||
|
||||
const nextResult = await next;
|
||||
assert.strictEqual(nextResult.value[0][0], 2);
|
||||
assert.strictEqual(pulls, 3);
|
||||
|
||||
shared.cancel();
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
Reference in new issue
Block a user