[whatwg/streams] Settle reader.[[closedPromise]] before performing close/error steps of read requests (#1102)

At least two implementations (#1100, MattiasBuelens/web-streams-polyfill#66) have made the (incorrect) assumption that `reader.[[closedPromise]]` will only be resolved or rejected if the promise is still pending. This is currently not the case: an async iterator may release its lock (and reject `reader.[[closedPromise]]`) right before the stream tries to resolve that same promise again.

With this PR, we now resolve or reject `reader.[[closedPromise]]` **before** going through all pending read requests and running their close or errors steps. This ensures that the stream is already done with all of its own state updates, and the read request's steps can observe and manipulate the "final" state of the stream. Thus, when the async iterator releases its lock, it will replace the `[[closedPromise]]` with a newly rejected promise instead.

Although this change is not observable for web authors (since all operations on a reader or async iterator have at least one microtask delay), it *may* be observable for other specifications (see [comment on #1100](https://github.com/whatwg/streams/issues/1100#issuecomment-761282151)).

There was also a small bug in the reference implementation of the WebIDL promise helpers. Calling `resolvePromise` or `rejectPromise` on a promise created with `promiseResolvedWith` or `promiseRejectedWith` would throw an error (such as "`promiseSideTable.get(p).resolve` is not a function"). If we attempt to resolve or reject a promise that is already resolved or rejected, we should do nothing instead.

- [ ] 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/1102


-- Commit Summary --

  * Settle reader.closed promise before closing/erroring read requests
  * Fix reference implementation of WebIDL promise helpers
  * Roll WPT

-- File Changes --

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

-- Patch Links --

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

https://github.com/whatwg/streams/pull/1102.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/1102

Received on Tuesday, 19 January 2021 00:21:16 UTC