Skip to content

Commit 96051ff

Browse files
MoonBalldanielleadams
authored andcommitted
lib: clean after the cancel algorithm throw error
PR-URL: #41366 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
1 parent 0b14352 commit 96051ff

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/internal/webstreams/readablestream.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1911,9 +1911,12 @@ function readableStreamDefaultControllerError(controller, error) {
19111911

19121912
function readableStreamDefaultControllerCancelSteps(controller, reason) {
19131913
resetQueue(controller);
1914-
const result = controller[kState].cancelAlgorithm(reason);
1915-
readableStreamDefaultControllerClearAlgorithms(controller);
1916-
return result;
1914+
try {
1915+
const result = controller[kState].cancelAlgorithm(reason);
1916+
return result;
1917+
} finally {
1918+
readableStreamDefaultControllerClearAlgorithms(controller);
1919+
}
19171920
}
19181921

19191922
function readableStreamDefaultControllerPullSteps(controller, readRequest) {

test/parallel/test-whatwg-readablestream.js

+30
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,36 @@ const {
8080
assert(r.locked);
8181
}
8282

83+
{
84+
// Throw error and return rejected promise in `cancel()` method
85+
// would execute same cleanup code
86+
const r1 = new ReadableStream({
87+
cancel: () => {
88+
return Promise.reject('Cancel Error');
89+
},
90+
});
91+
r1.cancel().finally(common.mustCall(() => {
92+
const controllerState = r1[kState].controller[kState];
93+
94+
assert.strictEqual(controllerState.pullAlgorithm, undefined);
95+
assert.strictEqual(controllerState.cancelAlgorithm, undefined);
96+
assert.strictEqual(controllerState.sizeAlgorithm, undefined);
97+
})).catch(() => {});
98+
99+
const r2 = new ReadableStream({
100+
cancel() {
101+
throw new Error('Cancel Error');
102+
}
103+
});
104+
r2.cancel().finally(common.mustCall(() => {
105+
const controllerState = r2[kState].controller[kState];
106+
107+
assert.strictEqual(controllerState.pullAlgorithm, undefined);
108+
assert.strictEqual(controllerState.cancelAlgorithm, undefined);
109+
assert.strictEqual(controllerState.sizeAlgorithm, undefined);
110+
})).catch(() => {});
111+
}
112+
83113
{
84114
const source = {
85115
start: common.mustCall((controller) => {

0 commit comments

Comments
 (0)