Skip to content

Commit

Permalink
Allow releasing the lock in the middle of close/error steps
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Oct 2, 2021
1 parent 5ba4701 commit 0ec648d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
21 changes: 12 additions & 9 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -2565,9 +2565,10 @@ the {{ReadableStream}}'s public API.
1. Perform ! [$ReadableStreamClose$](|stream|).
1. Let |reader| be |stream|.[=ReadableStream/[[reader]]=].
1. If |reader| is not undefined and |reader| [=implements=] {{ReadableStreamBYOBReader}},
1. [=list/For each=] |readIntoRequest| of |reader|.[=ReadableStreamBYOBReader/[[readIntoRequests]]=],
1. Perform |readIntoRequest|'s [=read-into request/close steps=], given undefined.
1. Let |readIntoRequests| be |reader|.[=ReadableStreamBYOBReader/[[readIntoRequests]]=].
1. Set |reader|.[=ReadableStreamBYOBReader/[[readIntoRequests]]=] to an empty [=list=].
1. [=list/For each=] |readIntoRequest| of |readIntoRequests|,
1. Perform |readIntoRequest|'s [=read-into request/close steps=], given undefined.
1. Let |sourceCancelPromise| be !
|stream|.[=ReadableStream/[[controller]]=].[$ReadableStreamController/[[CancelSteps]]$](|reason|).
1. Return the result of [=reacting=] to |sourceCancelPromise| with a fulfillment step that returns
Expand All @@ -2584,9 +2585,10 @@ the {{ReadableStream}}'s public API.
1. If |reader| is undefined, return.
1. [=Resolve=] |reader|.[=ReadableStreamGenericReader/[[closedPromise]]=] with undefined.
1. If |reader| [=implements=] {{ReadableStreamDefaultReader}},
1. [=list/For each=] |readRequest| of |reader|.[=ReadableStreamDefaultReader/[[readRequests]]=],
1. Perform |readRequest|'s [=read request/close steps=].
1. Let |readRequests| be |reader|.[=ReadableStreamDefaultReader/[[readRequests]]=].
1. Set |reader|.[=ReadableStreamDefaultReader/[[readRequests]]=] to an empty [=list=].
1. [=list/For each=] |readRequest| of |readRequests|,
1. Perform |readRequest|'s [=read request/close steps=].
</div>

<div algorithm>
Expand All @@ -2601,15 +2603,16 @@ the {{ReadableStream}}'s public API.
1. [=Reject=] |reader|.[=ReadableStreamGenericReader/[[closedPromise]]=] with |e|.
1. Set |reader|.[=ReadableStreamGenericReader/[[closedPromise]]=].\[[PromiseIsHandled]] to true.
1. If |reader| [=implements=] {{ReadableStreamDefaultReader}},
1. [=list/For each=] |readRequest| of |reader|.[=ReadableStreamDefaultReader/[[readRequests]]=],
1. Perform |readRequest|'s [=read request/error steps=], given |e|.
1. Let |readRequests| be |reader|.[=ReadableStreamDefaultReader/[[readRequests]]=].
1. Set |reader|.[=ReadableStreamDefaultReader/[[readRequests]]=] to a new empty [=list=].
1. [=list/For each=] |readRequest| of |readRequests|,
1. Perform |readRequest|'s [=read request/error steps=], given |e|.
1. Otherwise,
1. Assert: |reader| [=implements=] {{ReadableStreamBYOBReader}}.
1. [=list/For each=] |readIntoRequest| of
|reader|.[=ReadableStreamBYOBReader/[[readIntoRequests]]=],
1. Perform |readIntoRequest|'s [=read-into request/error steps=], given |e|.
1. Let |readIntoRequests| be |reader|.[=ReadableStreamBYOBReader/[[readIntoRequests]]=].
1. Set |reader|.[=ReadableStreamBYOBReader/[[readIntoRequests]]=] to a new empty [=list=].
1. [=list/For each=] |readIntoRequest| of |readIntoRequests|,
1. Perform |readIntoRequest|'s [=read-into request/error steps=], given |e|.
</div>

<div algorithm>
Expand Down
22 changes: 12 additions & 10 deletions reference-implementation/lib/abstract-ops/readable-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,11 @@ function ReadableStreamCancel(stream, reason) {

const reader = stream._reader;
if (reader !== undefined && ReadableStreamBYOBReader.isImpl(reader)) {
for (const readIntoRequest of reader._readIntoRequests) {
const readIntoRequests = reader._readIntoRequests;
reader._readIntoRequests = [];
for (const readIntoRequest of readIntoRequests) {
readIntoRequest.closeSteps(undefined);
}
reader._readIntoRequests = [];
}

const sourceCancelPromise = stream._controller[CancelSteps](reason);
Expand All @@ -743,10 +744,11 @@ function ReadableStreamClose(stream) {
resolvePromise(reader._closedPromise, undefined);

if (ReadableStreamDefaultReader.isImpl(reader)) {
for (const readRequest of reader._readRequests) {
const readRequests = reader._readRequests;
reader._readRequests = [];
for (const readRequest of readRequests) {
readRequest.closeSteps();
}
reader._readRequests = [];
}
}

Expand All @@ -766,19 +768,19 @@ function ReadableStreamError(stream, e) {
setPromiseIsHandledToTrue(reader._closedPromise);

if (ReadableStreamDefaultReader.isImpl(reader)) {
for (const readRequest of reader._readRequests) {
const readRequests = reader._readRequests;
reader._readRequests = [];
for (const readRequest of readRequests) {
readRequest.errorSteps(e);
}

reader._readRequests = [];
} else {
assert(ReadableStreamBYOBReader.isImpl(reader));

for (const readIntoRequest of reader._readIntoRequests) {
const readIntoRequests = reader._readIntoRequests;
reader._readIntoRequests = [];
for (const readIntoRequest of readIntoRequests) {
readIntoRequest.errorSteps(e);
}

reader._readIntoRequests = [];
}
}

Expand Down

0 comments on commit 0ec648d

Please sign in to comment.