Skip to content

Commit 01f4238

Browse files
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>
1 parent 537a20c commit 01f4238

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,15 +3860,15 @@ function readableByteStreamControllerFillReadRequestFromQueue(controller, readRe
38603860
}
38613861

38623862
function readableByteStreamControllerProcessReadRequestsUsingQueue(controller) {
3863-
const {
3864-
stream,
3865-
queueTotalSize,
3866-
} = controller[kState];
3863+
const state = controller[kState];
3864+
const { stream } = state;
38673865
const { reader } = stream[kState];
38683866
assert(isReadableStreamDefaultReader(reader));
38693867

38703868
while (reader[kState].readRequests.length > 0) {
3871-
if (queueTotalSize === 0) {
3869+
// Filling a read request dequeues a chunk, so re-read the queue size
3870+
// on every iteration.
3871+
if (state.queueTotalSize === 0) {
38723872
return;
38733873
}
38743874
readableByteStreamControllerFillReadRequestFromQueue(
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
// A released BYOB reader's partially filled pull-into is moved to the
4+
// queue on the next enqueue(). If a default reader is waiting with more
5+
// than one read request, the queue must only fill as many read requests
6+
// as it has chunks for, and the newly enqueued chunk must go to the next
7+
// read request.
8+
9+
const common = require('../common');
10+
const assert = require('assert');
11+
const { ReadableStream } = require('stream/web');
12+
13+
async function test(numReads) {
14+
let controller;
15+
const rs = new ReadableStream({
16+
type: 'bytes',
17+
start(c) {
18+
controller = c;
19+
},
20+
});
21+
22+
// Leave a pull-into with 1 of the required 2 bytes filled.
23+
const byobReader = rs.getReader({ mode: 'byob' });
24+
const byobRead = byobReader.read(new Uint8Array(2), { min: 2 });
25+
await new Promise(setImmediate);
26+
controller.enqueue(new Uint8Array([1]));
27+
byobReader.releaseLock();
28+
await assert.rejects(byobRead, { name: 'TypeError' });
29+
30+
const reader = rs.getReader();
31+
const reads = [];
32+
for (let i = 0; i < numReads; i++) {
33+
reads.push(reader.read());
34+
}
35+
36+
controller.enqueue(new Uint8Array([2]));
37+
controller.enqueue(new Uint8Array([3]));
38+
controller.close();
39+
40+
const results = await Promise.all(reads);
41+
const expected = [[1], [2], [3]].slice(0, numReads);
42+
assert.deepStrictEqual(results.map(({ value }) => [...value]), expected);
43+
44+
// Chunks that were not read yet are still in the queue.
45+
const rest = [];
46+
for (let result = await reader.read(); !result.done;
47+
result = await reader.read()) {
48+
rest.push(...result.value);
49+
}
50+
assert.deepStrictEqual(rest, [1, 2, 3].slice(numReads));
51+
}
52+
53+
(async () => {
54+
for (const numReads of [1, 2, 3]) {
55+
await test(numReads);
56+
}
57+
})().then(common.mustCall());

0 commit comments

Comments
 (0)