mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
stream: fix stale byte stream queue size
Re-read the byte controller's queue size before filling each pending read request. A released BYOB reader can leave a partially filled pull-into descriptor that is enqueued while a default reader has multiple pending reads. Caching the queue size causes the loop to dequeue from an empty queue after fulfilling the first read request. Add a regression test for one, two, and three pending reads and verify that any remaining queued bytes are preserved. Signed-off-by: Mattias Buelens <mattias@buelens.com> PR-URL: https://github.com/nodejs/node/pull/67499 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
03eab40fa4
commit
3a8a88e1cc
2 files changed
+62
-5
No files matched your search
@@ -3913,15 +3913,15 @@ function readableByteStreamControllerFillReadRequestFromQueue(controller, readRe
|
||||
}
|
||||
|
||||
function readableByteStreamControllerProcessReadRequestsUsingQueue(controller) {
|
||||
const {
|
||||
stream,
|
||||
queueTotalSize,
|
||||
} = controller[kState];
|
||||
const state = controller[kState];
|
||||
const { stream } = state;
|
||||
const { reader } = stream[kState];
|
||||
assert(isReadableStreamDefaultReader(reader));
|
||||
|
||||
while (reader[kState].readRequests.length > 0) {
|
||||
if (queueTotalSize === 0) {
|
||||
// Filling a read request dequeues a chunk, so re-read the queue size
|
||||
// on every iteration.
|
||||
if (state.queueTotalSize === 0) {
|
||||
return;
|
||||
}
|
||||
readableByteStreamControllerFillReadRequestFromQueue(
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
// A released BYOB reader's partially filled pull-into is moved to the
|
||||
// queue on the next enqueue(). If a default reader is waiting with more
|
||||
// than one read request, the queue must only fill as many read requests
|
||||
// as it has chunks for, and the newly enqueued chunk must go to the next
|
||||
// read request.
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { ReadableStream } = require('stream/web');
|
||||
|
||||
async function test(numReads) {
|
||||
let controller;
|
||||
const rs = new ReadableStream({
|
||||
type: 'bytes',
|
||||
start(c) {
|
||||
controller = c;
|
||||
},
|
||||
});
|
||||
|
||||
// Leave a pull-into with 1 of the required 2 bytes filled.
|
||||
const byobReader = rs.getReader({ mode: 'byob' });
|
||||
const byobRead = byobReader.read(new Uint8Array(2), { min: 2 });
|
||||
await new Promise(setImmediate);
|
||||
controller.enqueue(new Uint8Array([1]));
|
||||
byobReader.releaseLock();
|
||||
await assert.rejects(byobRead, { name: 'TypeError' });
|
||||
|
||||
const reader = rs.getReader();
|
||||
const reads = [];
|
||||
for (let i = 0; i < numReads; i++) {
|
||||
reads.push(reader.read());
|
||||
}
|
||||
|
||||
controller.enqueue(new Uint8Array([2]));
|
||||
controller.enqueue(new Uint8Array([3]));
|
||||
controller.close();
|
||||
|
||||
const results = await Promise.all(reads);
|
||||
const expected = [[1], [2], [3]].slice(0, numReads);
|
||||
assert.deepStrictEqual(results.map(({ value }) => [...value]), expected);
|
||||
|
||||
// Chunks that were not read yet are still in the queue.
|
||||
const rest = [];
|
||||
for (let result = await reader.read(); !result.done;
|
||||
result = await reader.read()) {
|
||||
rest.push(...result.value);
|
||||
}
|
||||
assert.deepStrictEqual(rest, [1, 2, 3].slice(numReads));
|
||||
}
|
||||
|
||||
(async () => {
|
||||
for (const numReads of [1, 2, 3]) {
|
||||
await test(numReads);
|
||||
}
|
||||
})().then(common.mustCall());
|
||||
Reference in new issue
Block a user