lib: clean after the cancel algorithm throw error

PR-URL: https://github.com/nodejs/node/pull/41366
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
This commit is contained in:
Gang Chen
2022-02-26 21:19:42 +08:00
committed by GitHub
parent 5f21a2b746
commit de77515a77
2 changed files with 36 additions and 3 deletions

View File

@@ -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) {

View File

@@ -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) => {