diff options
author | Gang Chen <13298548+MoonBall@users.noreply.github.com> | 2022-02-26 21:19:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-26 13:19:42 +0000 |
commit | de77515a7753404be28a5a4e2a51d74a0319b704 (patch) | |
tree | 2f1f5a42d231d2ff57fa4116d1247acd0a2db126 | |
parent | 5f21a2b746dbac63259d10cdb138753bb0bac293 (diff) | |
download | node-new-de77515a7753404be28a5a4e2a51d74a0319b704.tar.gz |
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>
-rw-r--r-- | lib/internal/webstreams/readablestream.js | 9 | ||||
-rw-r--r-- | test/parallel/test-whatwg-readablestream.js | 30 |
2 files changed, 36 insertions, 3 deletions
diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 0746d44171..bea1084bdd 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -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) { diff --git a/test/parallel/test-whatwg-readablestream.js b/test/parallel/test-whatwg-readablestream.js index 13261fe6b7..cef3eca6ed 100644 --- a/test/parallel/test-whatwg-readablestream.js +++ b/test/parallel/test-whatwg-readablestream.js @@ -81,6 +81,36 @@ const { } { + // 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) => { assert(controller instanceof ReadableStreamDefaultController); |