- From: Mattias Buelens <notifications@github.com>
- Date: Thu, 21 Feb 2019 12:22:25 -0800
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/996@github.com>
While reviewing #991, [I stumbled on a bug in `tee()`](https://github.com/whatwg/streams/issues/991#issuecomment-465814861).
For a single readable stream, we guarantee that we won't call `pull()` until the previous pull promise has resolved. But in the case of `tee()`, we use *the same pull algorithm* for both branches, so there's nothing stopping them from calling that function *simultaneously*. Since both branches are constructed with a default high water mark of 1, they'll start out calling `pull()`, so we will start **2 read operations instead of just 1**.
## Reproduction code
```js
let remainingChunks = ['a', 'b', 'c'];
const rs = new ReadableStream({
pull(c) {
console.log('enqueue', remainingChunks[0]);
c.enqueue(remainingChunks.shift());
if (remainingChunks.length === 0) {
console.log('close');
c.close();
}
}
}, {highWaterMark: 0});
const [branch1, branch2] = rs.tee();
```
## Expected output
```
enqueue a
```
Both branches have HWM = 1, so `tee()` should read one chunk and enqueue it on both branches.
## Actual output
```
enqueue a
enqueue b
```
Both branches call `pull()` at the same time, so we read two chunks and enqueue them on both branches. This pushes them (unnecessarily) over their HWM = 1.
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/996
Received on Thursday, 21 February 2019 20:22:47 UTC