Skip to content

Commit 384872f

Browse files
MoonBallsxa
authored andcommitted
lib: clean after the cancel algorithm throw error
PR-URL: #41366 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 5d010bc commit 384872f

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,9 +1911,12 @@ function readableStreamDefaultControllerError(controller, error) {
19111911

19121912
functionreadableStreamDefaultControllerCancelSteps(controller,reason){
19131913
resetQueue(controller);
1914-
constresult=controller[kState].cancelAlgorithm(reason);
1915-
readableStreamDefaultControllerClearAlgorithms(controller);
1916-
returnresult;
1914+
try{
1915+
constresult=controller[kState].cancelAlgorithm(reason);
1916+
returnresult;
1917+
}finally{
1918+
readableStreamDefaultControllerClearAlgorithms(controller);
1919+
}
19171920
}
19181921

19191922
functionreadableStreamDefaultControllerPullSteps(controller,readRequest){

‎test/parallel/test-whatwg-readablestream.js‎

Lines changed: 30 additions & 0 deletions
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+
constr1=newReadableStream({
87+
cancel: ()=>{
88+
returnPromise.reject('Cancel Error');
89+
},
90+
});
91+
r1.cancel().finally(common.mustCall(()=>{
92+
constcontrollerState=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+
constr2=newReadableStream({
100+
cancel(){
101+
thrownewError('Cancel Error');
102+
}
103+
});
104+
r2.cancel().finally(common.mustCall(()=>{
105+
constcontrollerState=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
constsource={
85115
start: common.mustCall((controller)=>{

0 commit comments

Comments
 (0)