[whatwg/streams] Fix cancelPromise in ReadableStreamTee being resolved twice (#1112)

[The following test](https://github.com/web-platform-tests/wpt/blob/e5e5e7a10cbb968b31c51ad87ce8a3da62bb1b34/streams/readable-streams/tee.any.js#L235-L246) is throwing an `AssertionError`:
```javascript
let controller;
const stream = new ReadableStream({ start(c) { controller = c; } });
const [branch1, branch2] = stream.tee();

controller.error("error");

branch1.cancel().catch(_=>_);
branch2.cancel().catch(_=>_);
```

This can be seen in our CI logs, e.g. [the latest run on `master`](https://github.com/whatwg/streams/runs/1905086996):
```
AssertionError: false == true
    at resolvePromise (eval at setup (/home/runner/work/streams/streams/reference-implementation/run-web-platform-tests.js:56:14), <anonymous>:6894:3)
    at eval (eval at setup (/home/runner/work/streams/streams/reference-implementation/run-web-platform-tests.js:56:14), <anonymous>:4831:5)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
  √ ReadableStream teeing: erroring a teed stream should error both branches
```
which refers to [this line in the reference implementation](https://github.com/whatwg/streams/blob/200c971563b1a695fce3eebe6dab45c348ff0ac0/reference-implementation/lib/helpers/webidl.js#L31):
```javascript
assert(stateIsPending(p) === true);
```
This indicates that we're attempting to resolve an already resolved promise. Although implementers are required to ignore such attempts, we try to avoid doing this in the specification, which is why we added this check in #1102. Unfortunately, it looks like we missed a case where this assertion fails...

This PR fixes it by checking if both branches have already been canceled in _"upon rejection of reader.[[closedPromise]]"_. If that's the case, then we will have already resolved `cancelPromise` and we don't need to resolve it again.

- [ ] At least two implementers are interested (and none opposed):
   * …
   * …
- [ ] [Tests](https://github.com/web-platform-tests/wpt) are written and can be reviewed and commented upon at:
   * …
- [ ] [Implementation bugs](https://github.com/whatwg/meta/blob/main/MAINTAINERS.md#handling-pull-requests) are filed:
   * Chrome: …
   * Firefox: …
   * Safari: …

(See [WHATWG Working Mode: Changes](https://whatwg.org/working-mode#changes) for more details.)

You can view, comment on, or merge this pull request online at:

  https://github.com/whatwg/streams/pull/1112


-- Commit Summary --

  * Fix cancelPromise in ReadableStreamTee being resolved twice
  * Roll WPT

-- File Changes --

    M index.bs (2)
    M reference-implementation/lib/abstract-ops/readable-streams.js (4)
    M reference-implementation/web-platform-tests (2)

-- Patch Links --

https://github.com/whatwg/streams/pull/1112.patch

https://github.com/whatwg/streams/pull/1112.diff


-- 
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/pull/1112

Received on Friday, 19 March 2021 17:22:28 UTC