Skip to content

Commit

Permalink
lib: clean after the cancel algorithm throw error
Browse files Browse the repository at this point in the history
PR-URL: #41366
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
MoonBall authored and sxa committed Mar 7, 2022
1 parent 5d010bc commit 384872f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
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

0 comments on commit 384872f

Please sign in to comment.