From 331bda71ff8f7cbda7c4dbb32b99438f8d3ccab8 Mon Sep 17 00:00:00 2001 From: Gang Chen Date: Fri, 31 Dec 2021 23:54:09 +0800 Subject: [PATCH 1/3] lib: clean after the cancel algorithm throw error --- lib/internal/webstreams/readablestream.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 79643c0e1e902f..9e2e10f6e3f7aa 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) { From c308d8aa4012580a25cd123d2104b8b990b41cb2 Mon Sep 17 00:00:00 2001 From: Gang Chen Date: Tue, 4 Jan 2022 22:57:11 +0800 Subject: [PATCH 2/3] add test --- test/parallel/test-whatwg-readablestream.js | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/parallel/test-whatwg-readablestream.js b/test/parallel/test-whatwg-readablestream.js index d27abdc75cea03..dce14181e1d311 100644 --- a/test/parallel/test-whatwg-readablestream.js +++ b/test/parallel/test-whatwg-readablestream.js @@ -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.deepEqual(controllerState.pullAlgorithm, undefined) + assert.deepEqual(controllerState.cancelAlgorithm, undefined) + assert.deepEqual(controllerState.sizeAlgorithm, undefined) + })).catch(() => {}); + + const r2 = new ReadableStream({ + cancel() { + throw Error('Cancel Error'); + } + }); + r2.cancel().finally(common.mustCall(() => { + const controllerState = r2[kState].controller[kState]; + + assert.deepEqual(controllerState.pullAlgorithm, undefined) + assert.deepEqual(controllerState.cancelAlgorithm, undefined) + assert.deepEqual(controllerState.sizeAlgorithm, undefined) + })).catch(() => {}); +} + { const source = { start: common.mustCall((controller) => { From 77b84aff58fc0e949e61ea147170fffc96e4f737 Mon Sep 17 00:00:00 2001 From: Gang Chen Date: Tue, 4 Jan 2022 23:06:33 +0800 Subject: [PATCH 3/3] fix lint error --- test/parallel/test-whatwg-readablestream.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-whatwg-readablestream.js b/test/parallel/test-whatwg-readablestream.js index dce14181e1d311..cc9d0f80da2c01 100644 --- a/test/parallel/test-whatwg-readablestream.js +++ b/test/parallel/test-whatwg-readablestream.js @@ -91,22 +91,22 @@ const { r1.cancel().finally(common.mustCall(() => { const controllerState = r1[kState].controller[kState]; - assert.deepEqual(controllerState.pullAlgorithm, undefined) - assert.deepEqual(controllerState.cancelAlgorithm, undefined) - assert.deepEqual(controllerState.sizeAlgorithm, undefined) + assert.strictEqual(controllerState.pullAlgorithm, undefined); + assert.strictEqual(controllerState.cancelAlgorithm, undefined); + assert.strictEqual(controllerState.sizeAlgorithm, undefined); })).catch(() => {}); const r2 = new ReadableStream({ cancel() { - throw Error('Cancel Error'); + throw new Error('Cancel Error'); } }); r2.cancel().finally(common.mustCall(() => { const controllerState = r2[kState].controller[kState]; - assert.deepEqual(controllerState.pullAlgorithm, undefined) - assert.deepEqual(controllerState.cancelAlgorithm, undefined) - assert.deepEqual(controllerState.sizeAlgorithm, undefined) + assert.strictEqual(controllerState.pullAlgorithm, undefined); + assert.strictEqual(controllerState.cancelAlgorithm, undefined); + assert.strictEqual(controllerState.sizeAlgorithm, undefined); })).catch(() => {}); }