Skip to content

Do not throw from ReadableByteStreamControllerClose - #1388

Open
shannonbooth wants to merge 2 commits into
whatwg:mainfrom
shannonbooth:close-byte-stream-partial-element
Open

shannonbooth wants to merge 2 commits into
whatwg:mainfrom
shannonbooth:close-byte-stream-partial-element

Conversation

@shannonbooth

@shannonbooth shannonbooth commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

ReadableByteStreamControllerClose already errors the stream when the first
pending pull-into descriptor holds only part of an element, so throwing
afterwards only matters to controller.close(). Move the throw there, so
the internal callers that use ! ("close a ReadableStream" and the
ReadableByteStreamTee close steps) see the stream become errored instead
of an abrupt completion.

Also skip RespondWithNewView on the BYOB branch in ReadableByteStreamTee
when closing it has errored it and cleared its pending pull-intos.

Fixes: #1387

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

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.
@shannonbooth

Copy link
Copy Markdown
Member Author

Huh, interestingly safari on my machine actually hits this issue

@shannonbooth

Copy link
Copy Markdown
Member Author

Huh, interestingly safari on my machine actually hits this issue

Though the WPT CI seems happy, so it must have been fixed in Webkit at some point recently

@MattiasBuelens

MattiasBuelens commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

I always found this to be a weird part of the specification... Is it really the byte source's fault that the BYOB reader is trying to do a multi-byte read(view), but there just aren't any more bytes? 🤔

I'm wondering if we should just get rid of the "throw e" in step 4.2.3, and let the byte source tear down "normally" while only the reader gets the error.

Edit: I'm moving this discussion back to #1387, since it would require quite a different PR.

@shannonbooth

Copy link
Copy Markdown
Member Author

I think there might be an option there! I'm looking at that. I'm also realising that I might not be fully fixing the problem with the current revision and am looking a little into interactions with tee

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.
@shannonbooth

Copy link
Copy Markdown
Member Author

@MattiasBuelens my previous version indeed had problems. Turns out it causes a crash in the new tests I pushed to web-platform-tests/wpt#64154. Also turns out that chrome is crashing on those tests (there looks to be an open issue for it). I think this version is a minimal fix perhaps?

@MattiasBuelens

Copy link
Copy Markdown
Collaborator

Yes, your fix looks pretty similar to what my LLM landed on. 😅 MattiasBuelens@45f3135

We could land this first to fix the problem while preserving the user-visible behavior of controller.close(), and later come back to #1389.

@shannonbooth

shannonbooth commented Oct 24, 2026 •

Copy link
Copy Markdown
Member Author

Yes, your fix looks pretty similar to what my LLM landed on. 😅 MattiasBuelens@45f3135

We could land this first to fix the problem while preserving the user-visible behavior of controller.close(), and later come back to #1389.

Put that commit message as MR description as I think it's more clear than my original. From some further LLM review of these changes, that found a different specification assertion failure, will split out an issue for that (edit: #1390)

@shannonbooth shannonbooth changed the title Do not assert that closing a readable byte stream cannot throw Do not throw from ReadableByteStreamControllerClose Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

"close a ReadableStream" asserts that ReadableByteStreamControllerClose can’t throw

2 participants