From c4f005884189d1c3caaaacc5329267b6484b03cb Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Sun, 21 Nov 2021 09:50:10 -0300 Subject: [PATCH] streams: use nextTick on close --- lib/internal/webstreams/readablestream.js | 52 ++++++++++++----------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index a88d166f886a94..a8f2031d5355bc 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -1403,32 +1403,36 @@ function readableStreamTee(stream, cloneForBranch2) { reading = true; const readRequest = { [kChunk](value) { - reading = false; - const value1 = value; - let value2 = value; - if (!canceled2 && cloneForBranch2) { - // Structured Clone - value2 = deserialize(serialize(value2)); - } - if (!canceled1) { - readableStreamDefaultControllerEnqueue( - branch1[kState].controller, - value1); - } - if (!canceled2) { - readableStreamDefaultControllerEnqueue( - branch2[kState].controller, - value2); - } + queueMicrotask(() => { + reading = false; + const value1 = value; + let value2 = value; + if (!canceled2 && cloneForBranch2) { + // Structured Clone + value2 = deserialize(serialize(value2)); + } + if (!canceled1) { + readableStreamDefaultControllerEnqueue( + branch1[kState].controller, + value1); + } + if (!canceled2) { + readableStreamDefaultControllerEnqueue( + branch2[kState].controller, + value2); + } + }); }, [kClose]() { - reading = false; - if (!canceled1) - readableStreamDefaultControllerClose(branch1[kState].controller); - if (!canceled2) - readableStreamDefaultControllerClose(branch2[kState].controller); - if (!canceled1 || !canceled2) - cancelPromise.resolve(); + process.nextTick(() => { + reading = false; + if (!canceled1) + readableStreamDefaultControllerClose(branch1[kState].controller); + if (!canceled2) + readableStreamDefaultControllerClose(branch2[kState].controller); + if (!canceled1 || !canceled2) + cancelPromise.resolve(); + }) }, [kError]() { reading = false;