From f72dc7328c8e2ba8738e411b6a48edc7fc65263b Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 24 Oct 2026 16:31:38 +0200 Subject: [PATCH 1/2] Do not assert that closing a readable byte stream cannot throw The "close a ReadableStream" algorithm for other specifications performs ! ReadableByteStreamControllerClose(), but that throws, after erroring the stream, when the first pending pull-into descriptor holds only part of an element. Fetch reaches this when a network body ends with a BYOB read pending, e.g., a 1-byte body read into a Uint16Array. --- index.bs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.bs b/index.bs index ba3fe866..fb755c4b 100644 --- a/index.bs +++ b/index.bs @@ -7000,8 +7000,13 @@ mark=] is greater than zero. To close a {{ReadableStream}} |stream|: 1. If |stream|.[=ReadableStream/[[controller]]=] [=implements=] {{ReadableByteStreamController}}, - 1. Perform ! + 1. Let |result| be [$ReadableByteStreamControllerClose$](|stream|.[=ReadableStream/[[controller]]=]). + 1. If |result| is an abrupt completion, return. +

This happens when the first [=pull-into descriptor=] in + |stream|.[=ReadableStream/[[controller]]=].[=ReadableByteStreamController/[[pendingPullIntos]]=] + has been filled with only part of an element, in which case |stream| has already been errored + with |result|.\[[Value]]. 1. If |stream|.[=ReadableStream/[[controller]]=].[=ReadableByteStreamController/[[pendingPullIntos]]=] is not [=list/is empty|empty=], perform ! [$ReadableByteStreamControllerRespond$](|stream|.[=ReadableStream/[[controller]]=], 0). From 9523b4f5b9c3c65cd1c3e325e9ef503c36cb2d9c Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 24 Oct 2026 20:32:03 +0200 Subject: [PATCH 2/2] Return instead of throwing from ReadableByteStreamControllerClose() Rather than handling the abrupt completion in "close a ReadableStream", make ReadableByteStreamControllerClose() return after erroring the stream, so that every caller performing it with ! is correct, including ReadableByteStreamTee(). controller.close() throws the stream's stored error instead, so its behavior is unchanged. ReadableByteStreamTee() also skips responding to the BYOB branch when closing it errored it, as erroring cleared its pending pull-into descriptors. --- index.bs | 17 ++++++++--------- .../lib/ReadableByteStreamController-impl.js | 4 ++++ .../lib/abstract-ops/readable-streams.js | 4 ++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/index.bs b/index.bs index fb755c4b..7a44b4eb 100644 --- a/index.bs +++ b/index.bs @@ -1870,7 +1870,9 @@ has the following [=struct/items=]: exception. 1. If [=this=].[=ReadableByteStreamController/[[stream]]=].[=ReadableStream/[[state]]=] is not "`readable`", throw a {{TypeError}} exception. - 1. Perform ? [$ReadableByteStreamControllerClose$]([=this=]). + 1. Perform ! [$ReadableByteStreamControllerClose$]([=this=]). + 1. If [=this=].[=ReadableByteStreamController/[[stream]]=].[=ReadableStream/[[state]]=] is + "`errored`", throw [=this=].[=ReadableByteStreamController/[[stream]]=].[=ReadableStream/[[storedError]]=].

@@ -2573,7 +2575,9 @@ create them does not matter. [$ReadableByteStreamControllerClose$](|otherBranch|.[=ReadableStream/[[controller]]=]). 1. If |chunk| is not undefined, 1. Assert: |chunk|.\[[ByteLength]] is 0. - 1. If |byobCanceled| is false, perform ! + 1. If |byobCanceled| is false and + |byobBranch|.[=ReadableStream/[[controller]]=].[=ReadableByteStreamController/[[pendingPullIntos]]=] + is not [=list/is empty|empty=], perform ! [$ReadableByteStreamControllerRespondWithNewView$](|byobBranch|.[=ReadableStream/[[controller]]=], |chunk|). 1. If |otherCanceled| is false and @@ -3249,7 +3253,7 @@ The following abstract operations support the implementation of the by |firstPendingPullInto|'s [=pull-into descriptor/element size=] is not 0, 1. Let |e| be a new {{TypeError}} exception. 1. Perform ! [$ReadableByteStreamControllerError$](|controller|, |e|). - 1. Throw |e|. + 1. Return. 1. Perform ! [$ReadableByteStreamControllerClearAlgorithms$](|controller|). 1. Perform ! [$ReadableStreamClose$](|stream|).
@@ -7000,13 +7004,8 @@ mark=] is greater than zero. To close a {{ReadableStream}} |stream|: 1. If |stream|.[=ReadableStream/[[controller]]=] [=implements=] {{ReadableByteStreamController}}, - 1. Let |result| be + 1. Perform ! [$ReadableByteStreamControllerClose$](|stream|.[=ReadableStream/[[controller]]=]). - 1. If |result| is an abrupt completion, return. -

This happens when the first [=pull-into descriptor=] in - |stream|.[=ReadableStream/[[controller]]=].[=ReadableByteStreamController/[[pendingPullIntos]]=] - has been filled with only part of an element, in which case |stream| has already been errored - with |result|.\[[Value]]. 1. If |stream|.[=ReadableStream/[[controller]]=].[=ReadableByteStreamController/[[pendingPullIntos]]=] is not [=list/is empty|empty=], perform ! [$ReadableByteStreamControllerRespond$](|stream|.[=ReadableStream/[[controller]]=], 0). diff --git a/reference-implementation/lib/ReadableByteStreamController-impl.js b/reference-implementation/lib/ReadableByteStreamController-impl.js index eac1a028..3b0fdd7b 100644 --- a/reference-implementation/lib/ReadableByteStreamController-impl.js +++ b/reference-implementation/lib/ReadableByteStreamController-impl.js @@ -25,6 +25,10 @@ exports.implementation = class ReadableByteStreamControllerImpl { } aos.ReadableByteStreamControllerClose(this); + + if (this._stream._state === 'errored') { + throw this._stream._storedError; + } } enqueue(chunk) { diff --git a/reference-implementation/lib/abstract-ops/readable-streams.js b/reference-implementation/lib/abstract-ops/readable-streams.js index 3127768c..31e0e3a9 100644 --- a/reference-implementation/lib/abstract-ops/readable-streams.js +++ b/reference-implementation/lib/abstract-ops/readable-streams.js @@ -631,7 +631,7 @@ function ReadableByteStreamTee(stream) { if (chunk !== undefined) { assert(chunk.byteLength === 0); - if (byobCanceled === false) { + if (byobCanceled === false && byobBranch._controller._pendingPullIntos.length > 0) { ReadableByteStreamControllerRespondWithNewView(byobBranch._controller, chunk); } if (otherCanceled === false && otherBranch._controller._pendingPullIntos.length > 0) { @@ -1276,7 +1276,7 @@ function ReadableByteStreamControllerClose(controller) { const e = new TypeError('Insufficient bytes to fill elements in the given buffer'); ReadableByteStreamControllerError(controller, e); - throw e; + return; } }