Skip to content

Commit 4e10155

Browse files
committed
streams: fix enqueue race condition on esm modules
streams: use nextTick on close fix: lint
1 parent 8731193 commit 4e10155

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

lib/internal/webstreams/readablestream.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1424,13 +1424,15 @@ function readableStreamTee(stream, cloneForBranch2) {
14241424
});
14251425
},
14261426
[kClose]() {
1427-
reading = false;
1428-
if (!canceled1)
1429-
readableStreamDefaultControllerClose(branch1[kState].controller);
1430-
if (!canceled2)
1431-
readableStreamDefaultControllerClose(branch2[kState].controller);
1432-
if (!canceled1 || !canceled2)
1433-
cancelPromise.resolve();
1427+
process.nextTick(() => {
1428+
reading = false;
1429+
if (!canceled1)
1430+
readableStreamDefaultControllerClose(branch1[kState].controller);
1431+
if (!canceled2)
1432+
readableStreamDefaultControllerClose(branch2[kState].controller);
1433+
if (!canceled1 || !canceled2)
1434+
cancelPromise.resolve();
1435+
});
14341436
},
14351437
[kError]() {
14361438
reading = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { ReadableStream } from 'stream/web';
3+
import assert from 'assert';
4+
5+
{
6+
// Test tee() with close in the nextTick after enqueue
7+
async function read(stream) {
8+
const chunks = [];
9+
for await (const chunk of stream)
10+
chunks.push(chunk);
11+
return Buffer.concat(chunks).toString();
12+
}
13+
14+
const [r1, r2] = new ReadableStream({
15+
start(controller) {
16+
process.nextTick(() => {
17+
controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));
18+
19+
process.nextTick(() => {
20+
controller.close();
21+
});
22+
});
23+
}
24+
}).tee();
25+
26+
(async () => {
27+
const [dataReader1, dataReader2] = await Promise.all([
28+
read(r1),
29+
read(r2),
30+
]);
31+
32+
assert.strictEqual(dataReader1, dataReader2);
33+
assert.strictEqual(dataReader1, 'foobar');
34+
assert.strictEqual(dataReader2, 'foobar');
35+
})().then(mustCall());
36+
}

0 commit comments

Comments
 (0)