Skip to content

TransformStream: survive controller API calls after a native sink tears down the readable - #33781

Merged
Jarred-Sumner merged 1 commit into
mainfrom
farm/c438d16f/transform-stream-abort-crash
Jul 8, 2026
Merged

Jarred-Sumner merged 1 commit into
mainfrom
farm/c438d16f/transform-stream-abort-crash

Conversation

@robobun

@robobun robobun commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

What

Bun.serve responding with req.body.pipeThrough(transform) where the transformer's async transform() calls controller.error() (a streaming validator rejecting a payload) segfaults the whole server process when the client aborts the upload mid-body. The same applies to controller.enqueue(), controller.terminate() and controller.desiredSize.

Repro

const srv = Bun.serve({
  port: 0, idleTimeout: 0,
  async fetch(req) {
    if (!req.body) return new Response("nobody");
    return new Response(req.body.pipeThrough(new TransformStream({
      async transform(_ch, c) { await new Promise(r => setTimeout(r, 2)); c.error(new Error("bad payload")); },
    })));
  },
});
// POST a few chunks of a content-length body, destroy the socket mid-upload. Within a few
// dozen aborts: SIGSEGV (release) / SIGABRT (asserts on).

Assert build:

ASSERTION FAILED: readable && readable->m_controllerKind == ControllerKind::Default
src/jsc/bindings/webcore/streams/TransformStreamOperations.cpp(34)
JSReadableStreamDefaultController *Bun::WebStreams::transformReadableController(JSTransformStream *)

Release build: Segmentation fault at address 0x88.

Cause

When a Bun native sink (Bun.serve response body, Bun.spawn stdin) consumes a TransformStream's readable side and the consumer aborts, the sink pump's finally step (rsisFinally / resumableReleaseReader / readDirectStreamCloseImpl in BunStreamSource.cpp) runs clearStreamControllerSlots() on the readable, setting m_controllerKind = None and clearing m_controller. The TransformStream still holds that readable via m_readable, so a later controller method call (from an async transform()/flush() that resumed after the abort, or from a queued sink reaction) walks transformReadableController(), which asserts ControllerKind::Default and then uncheckedDowncasts a null slot. Every caller immediately dereferences the result.

This is the TransformStream sibling of the tee-branch crash addressed in #33778.

Fix

Make both transformReadableController() copies null-safe (return nullptr when the readable's controller slot has been cleared) and make every call site skip the readable-side abstract operation on null: transformStreamError, transformStreamDefaultControllerEnqueue/Terminate, the desiredSize getter, and the four sink abort/close reaction handlers. The spec AOs (ReadableStreamDefaultControllerError/Close/Enqueue) would no-op on a non-Readable stream anyway; the writable-side error/unblock still runs. This mirrors the teeBranch*Controller approach from #33778.

Verification

New test in test/js/web/streams/streams.test.js passes ts.readable as stdin to a child process, kills the child, awaits the sink teardown (ts.readable.locked === false), then exercises all four controller members.

Without the fix: SIGABRT on the first action, 3/3 on the debug build.
With the fix: desiredSize returns null, enqueue() throws TypeError, terminate() and error() return normally; exit 0, 3/3.

  • test/js/third_party/wpt-streams/wpt-streams.test.ts: 1175 pass / 0 fail.
  • test/js/web/streams/streams.test.js: no new failures (2 pre-existing timeouts also present on main).
  • test/js/bun/spawn/spawn-stdin-readable-stream.test.ts: 25 pass / 0 fail.
  • test/js/web/streams/transform-stream-leak.test.ts: 3 pass / 0 fail.
  • Original Bun.serve + node:net client-abort loop (400 iterations): survives.

…rs down the readable

When a Bun native sink (Bun.serve response body, Bun.spawn stdin) consumes a
TransformStream's readable side and the consumer aborts, the sink pump's
finally step (rsisFinally / resumableReleaseReader / readDirectStreamCloseImpl)
runs clearStreamControllerSlots() on the readable, setting its controllerKind
to None and clearing m_controller. The TransformStream still holds that
readable via m_readable, so a later controller.error()/enqueue()/terminate()
or desiredSize access (from an async transform()/flush() that resumed after
the abort, or from a queued sink reaction) walks transformReadableController()
which asserts ControllerKind::Default and then dereferences a null controller.

On a release build this is a near-null deref (segfault at 0x88); on an
assert-enabled build it is a SIGABRT at TransformStreamOperations.cpp:34.
A remote client aborting an upload mid-stream is enough to trigger it.

Make both transformReadableController() copies null-safe (return nullptr when
the readable's controller slot has been cleared) and make every call site
skip the readable-side AO on null. The spec AOs would no-op on a terminal
readable anyway; the writable-side error/unblock still runs. This mirrors the
teeBranch*Controller null-safety added for the tee() sibling of this bug.
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 24 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 22de67e1-d071-404c-a2ca-850915ae3070

📥 Commits

Reviewing files that changed from the base of the PR and between 332f744 and f1a573d.

📒 Files selected for processing (3)
  • src/jsc/bindings/webcore/streams/JSTransformStreamDefaultController.cpp
  • src/jsc/bindings/webcore/streams/TransformStreamOperations.cpp
  • test/js/web/streams/streams.test.js

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the claude label Jul 8, 2026
@robobun

robobun commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 12:52 PM PT - Jul 8th, 2026

@robobun, your commit f1a573d is building: #70613

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Found 1 issue this PR may fix:

  1. Recurring segfault after POST to a Hono route mounting @hono/mcp StreamableHTTPTransport (Bun 1.3.13 and 1.3.14) #31004 - Recurring segfault after POST to a Hono route mounting @hono/mcp StreamableHTTPTransport; the MCP SDK uses TransformStreams internally and the crash pattern (segfault at null-like addresses during teardown) matches the scenario this PR fixes

If this is helpful, copy the block below into the PR description to auto-close this issue on merge.

Fixes #31004

🤖 Generated with Claude Code

@robobun

robobun commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

Not linking #31004: that issue is on Bun 1.3.13/1.3.14, which predates the C++ webstreams rewrite (#33193) this PR touches. The transformReadableController code path fixed here did not exist in those versions, and the crash offsets differ (0x0/0x8/0xD0 there vs 0x88 here). If the MCP SDK's TransformStream usage still crashes on current main after this lands, that would be a separate reproduction for #31004.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any bugs — the null-check pattern is applied consistently across all 8 call sites and mirrors #33778 — but this is a crash fix in the C++ WebStreams JSC bindings, so it's worth a human confirming the spec-AO no-op reasoning and exception-scope placement.

Extended reasoning...

Overview

The PR makes transformReadableController() (duplicated in JSTransformStreamDefaultController.cpp and TransformStreamOperations.cpp) return nullptr instead of asserting when the readable's controller slot has been cleared by a Bun native-sink teardown (clearStreamControllerSlots() in BunStreamSource.cpp). All eight call sites are updated to skip the readable-side abstract operation on null while still running the writable-side error/unblock. A subprocess-based regression test exercises desiredSize, enqueue, terminate, and error after Bun.spawn stdin teardown.

Security risks

None identified. This is defensive null-checking on an internal path; no user-controlled input validation, auth, or crypto is involved. The pre-fix behavior was a segfault (DoS), so the change strictly reduces attack surface.

Level of scrutiny

High. This is memory-safety-critical C++ in the JSC WebStreams bindings — a layer where exception-scope discipline, GC lifetime, and spec-AO semantics all interact. The change removes an ASSERT that encoded a (now-false) invariant, and moves RETURN_IF_EXCEPTION calls inside conditionals. While each edit is mechanically simple and the pattern mirrors the recently-landed tee-branch fix in #33778, the correctness depends on the claim that the spec AOs would no-op on a non-Readable stream anyway — which a human familiar with the streams implementation should confirm.

Other factors

  • Grep confirms all transformReadableController callers are covered; none were missed.
  • The test asserts exact per-action outcomes (not just "didn't crash") and checks signalCode: null, which is the right hang-guard pattern.
  • WPT streams (1175 pass) and related suites reported green in the description.
  • The removed ASSERT previously also checked readable for null; the new code dereferences readable->m_controllerKind unconditionally. That's fine because m_readable is set in initializeTransformStream and never cleared, but it's the kind of detail a maintainer should glance at.

@robobun

robobun commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

On the two points from the review:

m_readable non-null: JSTransformStream::m_readable is written exactly once, in initializeTransformStream (TransformStreamOperations.cpp:134), and never cleared anywhere in the subsystem. The only write is the .set() during construction; there is no .clear(). So stream->m_readable.get() is always non-null by the time any controller method or reaction can run.

Spec-AO no-op reasoning: the three AOs called through the now-nullable pointer all gate on the readable's state as their first step:

  • readableStreamDefaultControllerError returns immediately if m_state != Readable (JSReadableStreamDefaultController.cpp:598).
  • readableStreamDefaultControllerClose returns immediately if !CanCloseOrEnqueue, which checks m_state == Readable (JSReadableStreamDefaultController.cpp:514, 628).
  • readableStreamDefaultControllerGetDesiredSize returns nullopt/0 for Errored/Closed (JSReadableStreamDefaultController.cpp:610-617).

All three clearStreamControllerSlots callers also drive the stream to a terminal state on the same path (readDirectStreamCloseImpl writes Errored/Closed directly; rsisFinally/resumableReleaseReader call readableStreamCloseIfPossible or are preceded by a cancel), so the AOs would have early-returned had the controller still been present. Skipping them on null preserves the outcome.

Exception-scope placement: each RETURN_IF_EXCEPTION moved inside an if (controller) block guarded only the call it follows; no other throwing call precedes it in the then-branch, and the fallthrough path has no throwing call before the next check. WPT streams (1175/1175) and BUN_JSC_validateExceptionChecks=1 are clean.

@Jarred-Sumner
Jarred-Sumner merged commit 807493b into main Jul 8, 2026
76 of 77 checks passed
@Jarred-Sumner
Jarred-Sumner deleted the farm/c438d16f/transform-stream-abort-crash branch July 8, 2026 20:35
Jarred-Sumner pushed a commit that referenced this pull request Sep 14, 2026
…42609)

### Problem

- `Bun.serve()` sends a second `Response` around an already sent
`ReadableStream` as a `200` with an empty body. The `error` handler does
not run.
- A native sink consumer (`Bun.serve()`, a `fetch()` upload,
`Bun.write()`, `Bun.spawn()` stdin, S3, `HTMLRewriter`) pumps a
JS-backed stream in `BunStreamSource.cpp`. At the end `rsisFinally`
(`:853`) releases the reader and `directStreamOnClose` (`:662`) drops
the lock: `locked === false`. A `type: "direct"` stream, so every async
iterable body, is not disturbed either. `RequestContext.rs:3152` checks
the lock alone.
- Found by a comparison with node v26.3.0, not by a user report.

### Fix

- `rsisBegin` and `readDirectStream` call the new
`JSReadableStream::markConsumedAsBody()` once the pump holds the stream.
The stream stays locked after the pump lets go.
- Behaviour change: after such a consumer took a stream, `getReader()`,
`tee()`, `pipeTo()` and `cancel()` fail with a `TypeError`. Body mixin
methods (#42116) and natively wired sinks (#42477) already do this.
Neither is released.
- `pipeTo()`, async iteration and `Bun.readableStreamTo*()` do not reach
these pumps and still unlock.
- Verified: `web-stream-state.test.ts` (30 new tests fail on main),
`body.test.ts` (12), `serve-reused-response.test.ts` (2). Self-reviewed:
3 concerns, 2 addressed, 1 out of scope (Notes).

### Background

- A JSSink is a native sink (`HTTPResponseSink`, `FileSink`).
`JSSink::assign_to_stream` pumps a stream into it.
- `readStreamIntoSink` reads through a default reader.
`readDirectStream` gives the sink to the `pull()` of a direct stream.
- `m_consumedAsBody` (#42116) is a bit that `isReadableStreamLocked()`
includes and nothing clears. The fetch spec never releases the reader of
a body.

<details><summary>Notes</summary>

**The rule.** A consumer that takes a stream as a body keeps it locked:
the body mixin methods (#42116), `to_any_blob` lifts (#42516), natively
wired `ByteStream`/`FileReader` sinks (#42477), and now the two pumps. A
reader at the stream level releases its lock as the Streams spec says.
Checked on this branch: `pipeTo()`, `pipeThrough()`, `for await`,
`getReader()` + `releaseLock()`, `Bun.readableStreamToText()`,
`Bun.readableStreamToArrayBuffer()`, `stream.text()`, `stream.bytes()`,
`stream.json()`, `stream.blob()` all end with `locked === false`.

**Scope of the pumps.** `readStreamIntoSink` and `readDirectStream` have
one caller, `assignToStream`, which only
`JSSinkController__assignToStream` calls (Rust
`JSSink::assign_to_stream`: `HTTPResponseSink` and its TLS and HTTP/3
siblings, `FetchRequestBodySink`, `NetworkSink`, `FileSink`,
`RewriterPipe`).

**State on main.** The lock state after the hand-off depends on how the
pump ended. A clean end and a sink that closes early (client gone,
aborted upload, child exited) go through `rsisFinish` and release the
reader one microtask after the stream closes. A producer error goes
through `rsisAbrupt`, which orphans the reader, so that stream stays
locked. A direct stream is never disturbed.

**Repro (the user-visible part).**

```js
const stream = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode("payload")); c.close(); } });
const responses = [new Response(stream), new Response(stream)];
await using server = Bun.serve({ port: 0, fetch: () => responses.shift(), error: e => new Response(e.code, { status: 500 }) });
for (let i = 0; i < 2; i++) { const r = await fetch(server.url); console.log(r.status, JSON.stringify(await r.text())); }
// main:   200 "payload", 200 ""
// branch: 200 "payload", 500 "ERR_STREAM_CANNOT_PIPE"   (Stream already used, please create a new one)
```

For a direct stream `new Response(stream)` per request shows the same on
main: `200 "hello"`, `200 ""`, `500`. On this branch the second `new
Response(stream)` throws `Body object should not be disturbed or
locked`.

**Other observable changes.** `request.bodyUsed` after `fetch(request)`
with an async iterable body is now `true` (was `false`, the stream was
not disturbed). `Bun.readableStreamToText(stream)` on a stream a sink
holds rejects with "ReadableStream has already been used" in place of
"ReadableStream is locked" (same `ERR_INVALID_STATE`).

**Bare streams.** `fetch(url, { body: stream })`, `Bun.write(path,
stream)` and `Bun.spawn({ stdin: stream })` keep a bare stream locked
too. Natively wired sources already do this for the same calls
(`ReadableStream__lockNative` never unlocks, see "errors a Bun.file()
stream whose file does not open" from #42477).

**node v26.3.0.** `fetch(url, { method: "POST", body: jsStream, duplex:
"half" })`: afterwards `locked === true` and `getReader()` throws. The
same after an upload that an `AbortSignal` stopped. The other five
consumers are Bun APIs with no Node counterpart.

**The mark comes after the reader acquisition.** A stream that another
reader already holds is not marked. `Bun.serve()`, `fetch()`,
`Bun.write()` and `HTMLRewriter` reject such a stream before the pump.
`Bun.spawn()` stdin checks only `is_disturbed` (`stdio.rs:388`, `:536`),
so a locked stream reaches the pump there.

**Out of scope (the concern not addressed).** `rsisFinally` calls
`clearStreamControllerSlots` also when the pump never got the lock.
`Bun.spawn({ stdin: stream })` with a stream the caller holds through
`getReader()` reaches that: the caller's `reader.read()` then never
settles. This is the same on main and on this branch. #41532 rejects a
locked stdin stream before the pump.

**Not covered here.**
- A handler that drains or partly reads a stream with its own reader,
releases it, and then returns it in a `Response` still gets a `200` with
the empty or remaining body. #36110 covers that gate.
- Handing the same Node `Readable` or generator object to a second
consumer makes a new stream each time.

**Changed tests.** Three tests in `bun-write.test.js` (from #42114) read
the terminal state through `stream.getReader().closed`. One test in
`streams.test.js` (from #33781) polled `ts.readable.locked` to wait for
the sink teardown. Both observed the unlock as a means, not as the
subject. They use `finished()` from `node:stream/promises` now. The
`streams.test.js` test still proves the teardown ran: `desiredSize`
reads `null` only after the controller slot is cleared, and reads `0`
for a stream that is only closed.

**Fail before.** With `src/` from main and `bun bd`:
`web-stream-state.test.ts` 30 of 46 fail, `body.test.ts` 12 of 778 fail,
`serve-reused-response.test.ts` 2 of 9 fail, and the three
`bun-write.test.js` tests fail on the new `locked` assertion. With this
branch all pass. On main only the `Bun.serve()` row of "the stdout of a
running child" fails: the other four consumers wire a pipe-backed
`FileReader` natively.

**Built-in JS.** I searched `src/js` for code that touches a stream
after a native sink took it. The only `stream.cancel()` on a web stream
is `ReadableFromWeb._destroy`, which #42573 handles for the body mixin
case.

**Earlier reports of the same class.** #7001 (fixed in #7861) and #6860.

**Suites run on the debug build.**
`test/js/web/streams/{streams,streams-leak,readable-stream-blob-consumed,readable-stream-terminal-barrier-release,transform-stream-leak}`,
`test/js/third_party/wpt-streams`,
`test/js/web/fetch/{body,body-stream,body-stream-excess,body-clone,body-async-iterator,body-mixin-errors,blob-write,fetch,fetch.stream,fetch-backpressure,fetch-abort-stream-body,fetch-stream-cancel-leak,fetch-redirect,response}`,
`test/js/web/request/request`,
`test/js/bun/http/{serve,bun-server,serve-reused-response,serve-direct-readable-stream,serve-body-leak,serve-pending-promise-abort-leak,async-iterator-stream,serve-async-stream-client-abort,serve-error-handler-stream,serve-response-stream-sink-leak,serve-stream-body-error,serve-stream-reject-flush-leak}`,
`test/js/bun/spawn/{spawn,spawn-stdin-readable-stream}`,
`test/js/bun/io/bun-write`,
`test/js/workerd/{html-rewriter,html-rewriter-leak}`,
`test/js/bun/s3/{s3-stream-cancel-leak,s3-stream-error-gc,s3-upload-stream-gc,s3-write-to-file-sync-close,s3-connection-close}`,
`test/js/node/stream/{node-stream,web-stream-state}`,
`test/js/node/async_hooks/AsyncLocalStorage`,
`test/regression/issue/07001`. The failures that remain also fail with
`src/` from main in this container: IPv6, tests that need a non-root
user, external hosts, and 5 s timeouts of the debug build.

</details>

<!-- robobun:evidence:begin -->

---

**no test proof** · iteration 0 · platform-specific test(s) that do not
run on this machine, deferring to CI, which covers all platforms:
test/js/web/streams/streams.test.js, test/js/bun/io/bun-write.test.js

<!-- robobun:evidence:end -->
usrbinkat pushed a commit to usrbinkat/bun that referenced this pull request Sep 15, 2026
…ven-sh#42609)

### Problem

- `Bun.serve()` sends a second `Response` around an already sent
`ReadableStream` as a `200` with an empty body. The `error` handler does
not run.
- A native sink consumer (`Bun.serve()`, a `fetch()` upload,
`Bun.write()`, `Bun.spawn()` stdin, S3, `HTMLRewriter`) pumps a
JS-backed stream in `BunStreamSource.cpp`. At the end `rsisFinally`
(`:853`) releases the reader and `directStreamOnClose` (`:662`) drops
the lock: `locked === false`. A `type: "direct"` stream, so every async
iterable body, is not disturbed either. `RequestContext.rs:3152` checks
the lock alone.
- Found by a comparison with node v26.3.0, not by a user report.

### Fix

- `rsisBegin` and `readDirectStream` call the new
`JSReadableStream::markConsumedAsBody()` once the pump holds the stream.
The stream stays locked after the pump lets go.
- Behaviour change: after such a consumer took a stream, `getReader()`,
`tee()`, `pipeTo()` and `cancel()` fail with a `TypeError`. Body mixin
methods (oven-sh#42116) and natively wired sinks (oven-sh#42477) already do this.
Neither is released.
- `pipeTo()`, async iteration and `Bun.readableStreamTo*()` do not reach
these pumps and still unlock.
- Verified: `web-stream-state.test.ts` (30 new tests fail on main),
`body.test.ts` (12), `serve-reused-response.test.ts` (2). Self-reviewed:
3 concerns, 2 addressed, 1 out of scope (Notes).

### Background

- A JSSink is a native sink (`HTTPResponseSink`, `FileSink`).
`JSSink::assign_to_stream` pumps a stream into it.
- `readStreamIntoSink` reads through a default reader.
`readDirectStream` gives the sink to the `pull()` of a direct stream.
- `m_consumedAsBody` (oven-sh#42116) is a bit that `isReadableStreamLocked()`
includes and nothing clears. The fetch spec never releases the reader of
a body.

<details><summary>Notes</summary>

**The rule.** A consumer that takes a stream as a body keeps it locked:
the body mixin methods (oven-sh#42116), `to_any_blob` lifts (oven-sh#42516), natively
wired `ByteStream`/`FileReader` sinks (oven-sh#42477), and now the two pumps. A
reader at the stream level releases its lock as the Streams spec says.
Checked on this branch: `pipeTo()`, `pipeThrough()`, `for await`,
`getReader()` + `releaseLock()`, `Bun.readableStreamToText()`,
`Bun.readableStreamToArrayBuffer()`, `stream.text()`, `stream.bytes()`,
`stream.json()`, `stream.blob()` all end with `locked === false`.

**Scope of the pumps.** `readStreamIntoSink` and `readDirectStream` have
one caller, `assignToStream`, which only
`JSSinkController__assignToStream` calls (Rust
`JSSink::assign_to_stream`: `HTTPResponseSink` and its TLS and HTTP/3
siblings, `FetchRequestBodySink`, `NetworkSink`, `FileSink`,
`RewriterPipe`).

**State on main.** The lock state after the hand-off depends on how the
pump ended. A clean end and a sink that closes early (client gone,
aborted upload, child exited) go through `rsisFinish` and release the
reader one microtask after the stream closes. A producer error goes
through `rsisAbrupt`, which orphans the reader, so that stream stays
locked. A direct stream is never disturbed.

**Repro (the user-visible part).**

```js
const stream = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode("payload")); c.close(); } });
const responses = [new Response(stream), new Response(stream)];
await using server = Bun.serve({ port: 0, fetch: () => responses.shift(), error: e => new Response(e.code, { status: 500 }) });
for (let i = 0; i < 2; i++) { const r = await fetch(server.url); console.log(r.status, JSON.stringify(await r.text())); }
// main:   200 "payload", 200 ""
// branch: 200 "payload", 500 "ERR_STREAM_CANNOT_PIPE"   (Stream already used, please create a new one)
```

For a direct stream `new Response(stream)` per request shows the same on
main: `200 "hello"`, `200 ""`, `500`. On this branch the second `new
Response(stream)` throws `Body object should not be disturbed or
locked`.

**Other observable changes.** `request.bodyUsed` after `fetch(request)`
with an async iterable body is now `true` (was `false`, the stream was
not disturbed). `Bun.readableStreamToText(stream)` on a stream a sink
holds rejects with "ReadableStream has already been used" in place of
"ReadableStream is locked" (same `ERR_INVALID_STATE`).

**Bare streams.** `fetch(url, { body: stream })`, `Bun.write(path,
stream)` and `Bun.spawn({ stdin: stream })` keep a bare stream locked
too. Natively wired sources already do this for the same calls
(`ReadableStream__lockNative` never unlocks, see "errors a Bun.file()
stream whose file does not open" from oven-sh#42477).

**node v26.3.0.** `fetch(url, { method: "POST", body: jsStream, duplex:
"half" })`: afterwards `locked === true` and `getReader()` throws. The
same after an upload that an `AbortSignal` stopped. The other five
consumers are Bun APIs with no Node counterpart.

**The mark comes after the reader acquisition.** A stream that another
reader already holds is not marked. `Bun.serve()`, `fetch()`,
`Bun.write()` and `HTMLRewriter` reject such a stream before the pump.
`Bun.spawn()` stdin checks only `is_disturbed` (`stdio.rs:388`, `:536`),
so a locked stream reaches the pump there.

**Out of scope (the concern not addressed).** `rsisFinally` calls
`clearStreamControllerSlots` also when the pump never got the lock.
`Bun.spawn({ stdin: stream })` with a stream the caller holds through
`getReader()` reaches that: the caller's `reader.read()` then never
settles. This is the same on main and on this branch. oven-sh#41532 rejects a
locked stdin stream before the pump.

**Not covered here.**
- A handler that drains or partly reads a stream with its own reader,
releases it, and then returns it in a `Response` still gets a `200` with
the empty or remaining body. oven-sh#36110 covers that gate.
- Handing the same Node `Readable` or generator object to a second
consumer makes a new stream each time.

**Changed tests.** Three tests in `bun-write.test.js` (from oven-sh#42114) read
the terminal state through `stream.getReader().closed`. One test in
`streams.test.js` (from oven-sh#33781) polled `ts.readable.locked` to wait for
the sink teardown. Both observed the unlock as a means, not as the
subject. They use `finished()` from `node:stream/promises` now. The
`streams.test.js` test still proves the teardown ran: `desiredSize`
reads `null` only after the controller slot is cleared, and reads `0`
for a stream that is only closed.

**Fail before.** With `src/` from main and `bun bd`:
`web-stream-state.test.ts` 30 of 46 fail, `body.test.ts` 12 of 778 fail,
`serve-reused-response.test.ts` 2 of 9 fail, and the three
`bun-write.test.js` tests fail on the new `locked` assertion. With this
branch all pass. On main only the `Bun.serve()` row of "the stdout of a
running child" fails: the other four consumers wire a pipe-backed
`FileReader` natively.

**Built-in JS.** I searched `src/js` for code that touches a stream
after a native sink took it. The only `stream.cancel()` on a web stream
is `ReadableFromWeb._destroy`, which oven-sh#42573 handles for the body mixin
case.

**Earlier reports of the same class.** oven-sh#7001 (fixed in oven-sh#7861) and oven-sh#6860.

**Suites run on the debug build.**
`test/js/web/streams/{streams,streams-leak,readable-stream-blob-consumed,readable-stream-terminal-barrier-release,transform-stream-leak}`,
`test/js/third_party/wpt-streams`,
`test/js/web/fetch/{body,body-stream,body-stream-excess,body-clone,body-async-iterator,body-mixin-errors,blob-write,fetch,fetch.stream,fetch-backpressure,fetch-abort-stream-body,fetch-stream-cancel-leak,fetch-redirect,response}`,
`test/js/web/request/request`,
`test/js/bun/http/{serve,bun-server,serve-reused-response,serve-direct-readable-stream,serve-body-leak,serve-pending-promise-abort-leak,async-iterator-stream,serve-async-stream-client-abort,serve-error-handler-stream,serve-response-stream-sink-leak,serve-stream-body-error,serve-stream-reject-flush-leak}`,
`test/js/bun/spawn/{spawn,spawn-stdin-readable-stream}`,
`test/js/bun/io/bun-write`,
`test/js/workerd/{html-rewriter,html-rewriter-leak}`,
`test/js/bun/s3/{s3-stream-cancel-leak,s3-stream-error-gc,s3-upload-stream-gc,s3-write-to-file-sync-close,s3-connection-close}`,
`test/js/node/stream/{node-stream,web-stream-state}`,
`test/js/node/async_hooks/AsyncLocalStorage`,
`test/regression/issue/07001`. The failures that remain also fail with
`src/` from main in this container: IPv6, tests that need a non-root
user, external hosts, and 5 s timeouts of the debug build.

</details>

<!-- robobun:evidence:begin -->

---

**no test proof** · iteration 0 · platform-specific test(s) that do not
run on this machine, deferring to CI, which covers all platforms:
test/js/web/streams/streams.test.js, test/js/bun/io/bun-write.test.js

<!-- robobun:evidence:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants