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

webstreams: clean after the cancel algorithm of readablestream throw error #41366

Merged
merged 3 commits into from
Feb 26, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -1911,9 +1911,12 @@ function readableStreamDefaultControllerError(controller, error) {

function readableStreamDefaultControllerCancelSteps(controller, reason) {
resetQueue(controller);
const result = controller[kState].cancelAlgorithm(reason);
readableStreamDefaultControllerClearAlgorithms(controller);
return result;
try {
const result = controller[kState].cancelAlgorithm(reason);
return result;
} finally {
readableStreamDefaultControllerClearAlgorithms(controller);
}
}

function readableStreamDefaultControllerPullSteps(controller, readRequest) {
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-whatwg-readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ const {
assert(r.locked);
}

{
// Throw error and return rejected promise in `cancel()` method
// would execute same cleanup code
const r1 = new ReadableStream({
cancel: () => {
return Promise.reject('Cancel Error');
},
});
r1.cancel().finally(common.mustCall(() => {
const controllerState = r1[kState].controller[kState];

assert.strictEqual(controllerState.pullAlgorithm, undefined);
assert.strictEqual(controllerState.cancelAlgorithm, undefined);
assert.strictEqual(controllerState.sizeAlgorithm, undefined);
})).catch(() => {});

const r2 = new ReadableStream({
cancel() {
throw new Error('Cancel Error');
}
});
r2.cancel().finally(common.mustCall(() => {
const controllerState = r2[kState].controller[kState];

assert.strictEqual(controllerState.pullAlgorithm, undefined);
assert.strictEqual(controllerState.cancelAlgorithm, undefined);
assert.strictEqual(controllerState.sizeAlgorithm, undefined);
})).catch(() => {});
}

{
const source = {
start: common.mustCall((controller) => {
Expand Down