Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WritableStream: clear algorithms once they will no longer be called #940

Merged
merged 4 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -3339,7 +3339,7 @@ nothrow>WritableStreamDealWithRejection ( <var>stream</var>, <var>error</var> )<
1. Repeat for each _writeRequest_ that is an element of _stream_.[[writeRequests]],
1. <a>Reject</a> _writeRequest_ with _storedError_.
1. Set _stream_.[[writeRequests]] to an empty List.
1. if _stream_.[[pendingAbortRequest]] is *undefined*,
1. If _stream_.[[pendingAbortRequest]] is *undefined*,
1. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(_stream_).
1. Return.
1. Let _abortRequest_ be _stream_.[[pendingAbortRequest]].
Expand Down Expand Up @@ -3947,7 +3947,9 @@ used polymorphically.
<var>reason</var> )</h5>

<emu-alg>
1. Return the result of performing *this*.[[abortAlgorithm]], passing _reason_.
1. Let _result_ be the result of performing *this*.[[abortAlgorithm]], passing _reason_.
1. Perform ! WritableStreamDefaultControllerClearAlgorithms(*this*).
1. Return _result_.
</emu-alg>

<h5 id="ws-default-controller-private-error">\[[ErrorSteps]]()</h5>
Expand Down Expand Up @@ -4015,6 +4017,26 @@ throws>SetUpWritableStreamDefaultControllerFromUnderlyingSink ( <var>stream</var
_closeAlgorithm_, _abortAlgorithm_, _highWaterMark_, _sizeAlgorithm_).
</emu-alg>

<h4 id="writable-stream-default-controller-clear-algorithms" aoid="WritableStreamDefaultControllerClearAlgorithms"
nothrow>WritableStreamDefaultControllerClearAlgorithms ( <var>controller</var> )</h4>

This abstract operation is called once the stream is closed or errored and the algorithms will not be executed any more.
By removing the algorithm references it permits the <a>underlying sink</a> object to be garbage collected even if the
{{WritableStream}} itself is still referenced.

<p class="note">The results of this algorithm are not currently observable, but could become so if JavaScript eventually
adds <a href="https://github.com/tc39/proposal-weakrefs/">weak references</a>. But even without that factor,
implementations will likely want to include similar steps.</p>

<p class="note">This operation will be performed multiple times in some edge cases. After the first time it will do
nothing.</p>

<emu-alg>
1. Set _controller_.[[writeAlgorithm]] to *undefined*.
1. Set _controller_.[[closeAlgorithm]] to *undefined*.
1. Set _controller_.[[abortAlgorithm]] to *undefined*.
</emu-alg>

<h4 id="writable-stream-default-controller-close" aoid="WritableStreamDefaultControllerClose"
nothrow>WritableStreamDefaultControllerClose ( <var>controller</var> )</h4>

Expand Down Expand Up @@ -4095,6 +4117,7 @@ nothrow>WritableStreamDefaultControllerProcessClose ( <var>controller</var> )</h
1. Perform ! DequeueValue(_controller_).
1. Assert: _controller_.[[queue]] is empty.
1. Let _sinkClosePromise_ be the result of performing _controller_.[[closeAlgorithm]].
1. Perform ! WritableStreamDefaultControllerClearAlgorithms(_controller_).
1. <a>Upon fulfillment</a> of _sinkClosePromise_,
1. Perform ! WritableStreamFinishInFlightClose(_stream_).
1. <a>Upon rejection</a> of _sinkClosePromise_ with reason _reason_,
Expand All @@ -4118,6 +4141,7 @@ nothrow>WritableStreamDefaultControllerProcessWrite ( <var>controller</var>, <va
1. Perform ! WritableStreamUpdateBackpressure(_stream_, _backpressure_).
1. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(_controller_).
1. <a>Upon rejection</a> of _sinkWritePromise_ with _reason_,
1. Perform ! WritableStreamDefaultControllerClearAlgorithms(_controller_).
1. Perform ! WritableStreamFinishInFlightWriteWithError(_stream_, _reason_).
</emu-alg>

Expand All @@ -4135,6 +4159,7 @@ nothrow>WritableStreamDefaultControllerError ( <var>controller</var>, <var>error
<emu-alg>
1. Let _stream_ be _controller_.[[controlledWritableStream]].
1. Assert: _stream_.[[state]] is `"writable"`.
1. Perform ! WritableStreamDefaultControllerClearAlgorithms(_controller_).
1. Perform ! WritableStreamStartErroring(_stream_, _error_).
</emu-alg>

Expand Down
16 changes: 15 additions & 1 deletion reference-implementation/lib/writable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@ class WritableStreamDefaultController {
}

[AbortSteps](reason) {
return this._abortAlgorithm(reason);
const result = this._abortAlgorithm(reason);
WritableStreamDefaultControllerClearAlgorithms(this);
return result;
}

[ErrorSteps]() {
Expand Down Expand Up @@ -798,6 +800,13 @@ function SetUpWritableStreamDefaultControllerFromUnderlyingSink(stream, underlyi
abortAlgorithm, highWaterMark, sizeAlgorithm);
}

// ClearAlgorithms may be called twice. Erroring the same stream in multiple ways will often result in redundant calls.
function WritableStreamDefaultControllerClearAlgorithms(controller) {
controller._writeAlgorithm = undefined;
controller._closeAlgorithm = undefined;
controller._abortAlgorithm = undefined;
}

function WritableStreamDefaultControllerClose(controller) {
EnqueueValueWithSize(controller, 'close', 0);
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
Expand Down Expand Up @@ -885,6 +894,7 @@ function WritableStreamDefaultControllerProcessClose(controller) {
assert(controller._queue.length === 0);

const sinkClosePromise = controller._closeAlgorithm();
WritableStreamDefaultControllerClearAlgorithms(controller);
sinkClosePromise.then(
() => {
WritableStreamFinishInFlightClose(stream);
Expand Down Expand Up @@ -919,6 +929,9 @@ function WritableStreamDefaultControllerProcessWrite(controller, chunk) {
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
},
reason => {
if (stream._state === 'writable') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check does not appear in the spec text. I believe it should be removed from the reference implementation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Actually this check is required, and I failed to add it to the spec text. Without this check, if a write() rejects while an abort() is already in progress, the abortAlgorithm will be cleared too early and so when the time comes to call the underlying source abort() it will fail.

WritableStreamDefaultControllerClearAlgorithms(controller);
}
WritableStreamFinishInFlightWriteWithError(stream, reason);
}
)
Expand All @@ -937,6 +950,7 @@ function WritableStreamDefaultControllerError(controller, error) {

assert(stream._state === 'writable');

WritableStreamDefaultControllerClearAlgorithms(controller);
WritableStreamStartErroring(stream, error);
}

Expand Down