From 5c23d670a37e33517403087d64869acbf93bccde Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 26 Nov 2023 11:11:42 +0100 Subject: [PATCH] Fix bug with readable streams that have been detached (#7308) Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/js/builtins/ReadableStreamInternals.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/js/builtins/ReadableStreamInternals.ts b/src/js/builtins/ReadableStreamInternals.ts index 36f049f3ec2ef5..eee2c41864eb00 100644 --- a/src/js/builtins/ReadableStreamInternals.ts +++ b/src/js/builtins/ReadableStreamInternals.ts @@ -1259,10 +1259,11 @@ export function readableStreamError(stream, error) { } export function readableStreamDefaultControllerShouldCallPull(controller) { - const stream = $getByIdDirectPrivate(controller, "controlledReadableStream"); - if (!$readableStreamDefaultControllerCanCloseOrEnqueue(controller)) return false; if (!($getByIdDirectPrivate(controller, "started") === 1)) return false; + + const stream = $getByIdDirectPrivate(controller, "controlledReadableStream"); + if ( (!$isReadableStreamLocked(stream) || !$getByIdDirectPrivate($getByIdDirectPrivate(stream, "reader"), "readRequests")?.isNotEmpty()) && @@ -1482,10 +1483,17 @@ export function readableStreamReaderGenericRelease(reader) { } export function readableStreamDefaultControllerCanCloseOrEnqueue(controller) { - return ( - !$getByIdDirectPrivate(controller, "closeRequested") && - $getByIdDirectPrivate($getByIdDirectPrivate(controller, "controlledReadableStream"), "state") === $streamReadable - ); + if ($getByIdDirectPrivate(controller, "closeRequested")) { + return false; + } + + const controlledReadableStream = $getByIdDirectPrivate(controller, "controlledReadableStream"); + + if (!$isObject(controlledReadableStream)) { + return false; + } + + return $getByIdDirectPrivate(controlledReadableStream, "state") === $streamReadable; } export function lazyLoadStream(stream, autoAllocateChunkSize) {