Skip to content

runtime: observe bodyUsed and match workerd's body errors (#115, #116, #117) - #124

Merged
ryuzdev merged 1 commit into
mainfrom
fix/workerd-diff-bodies
Nov 22, 2026
Merged

ryuzdev merged 1 commit into
mainfrom
fix/workerd-diff-bodies

Conversation

@ryuzdev

@ryuzdev ryuzdev commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Closes #115, #116, #117.

Three body-lifecycle divergences the workerd differential harness found, fixed
in crates/cello/js/harness.js, with each repro added to the gate
(scripts/workerd-diff).

#115 — bodyUsed observes the stream

request.bodyUsed / response.bodyUsed were plain state that only the
runtime's own consumers set, so a Worker that held a reader, or had read part
of a live request body, still saw false and any guard re-checking the flag
mid-stream was bypassed. bodyUsed is now a getter-only prototype accessor on
Request and Response (spec: true when the body is non-null and its stream
is disturbed), as workerd's readonly property is, with _bodyUsed as the
explicit half that only runtime code marks through __markBodyUsed
(buffered bodies, host-stream transfers, cache.put()). Merely holding a reader is not a disturbance — workerd
reports false there, and the new case pins that too.

#116 — clone() message

Both clones still refuse a consumed, disturbed or locked body, but now throw
workerd's TypeError: This ReadableStream is currently locked to a reader,
instead of Body has already been consumed. Workerd gates on the stream
being locked (its own Body consumers leave it locked, which is why a
consumed body reports this message); cello keeps the stricter gate, because
after a manual read plus releaseLock() workerd lets the clone through and
hands both sides empty bytes, dropping the payload already read. That
difference is documented in place; the refusal itself is unchanged.

#117 — non-byte body chunks

text(), json(), formData(), blob(), arrayBuffer(), bytes() and
a subrequest body all drain through __drainBody. A string chunk made
length NaN, so the target array was empty and bytes.set("alpha", 0)
threw the RangeError: offset is out of bounds the issue reports. The drain
now validates each chunk and rejects with
TypeError: This ReadableStream did not return bytes. before any byte is
returned, exactly as workerd does; ArrayBuffer and every
ArrayBufferView chunk are accepted.

Request.prototype.bytes / Response.prototype.bytes did not exist (Fetch's
undecoded-bytes accessor); they do now, so the bytes() consumer path is
covered like the others.

A chunk's window is read from internal slots only: the drain classifies the
chunk by its prototype chain and takes buffer/byteOffset/byteLength
from the captured %TypedArray% / DataView prototype getters, so own
byteLength/byteOffset/buffer properties a guest can define on a view
cannot pad the body, read another window, or throw.

The response/host path is unchanged on purpose: with string chunks it writes
their UTF-8 bytes, which is what workerd does (verified: alphabeta).
Cello's host pump already settles a mid-stream body error by closing the
host stream — a returning Worker's chunk is written, then the response is
truncated and the connection closed (observed: curl returns the partial
body in 29 ms, no hang).

Tests

  • Six new agreeing cases: body-used-reader (drains every chunk),
    res-body-used, clone-consumed, string-chunks, string-chunks-response,
    body-chunk-shadows. Full gate: 31/31.
  • Fail-before, with crates/cello/js/harness.js reverted and the new cases
    in place:
    • body-used-reader: during/after false vs workerd true.
    • res-body-used: same, for buffered and streamed bodies.
    • clone-consumed: Body has already been consumed vs workerd's message.
    • string-chunks: 500 (no bytes(); text() reported
      RangeError: offset is out of bounds, the transcript in workerd diff: a Response body built from string chunks throws RangeError instead of TypeError #117).
    • body-chunk-shadows: cello padded the body with 4093 NULs from a
      shadowed byteLength ("abc\u0000…), workerd "abc".
  • cargo test --profile ci --locked -p cello, clippy, cargo fmt --check,
    node scripts/check-docs.mjs.
  • Changelog: ## Unreleased in docs/changelog.md.

Not changed (found, out of scope)

  • new Response(["alpha"]).text(): workerd builds a byte stream from the
    iterable and throws the same TypeError; cello stringifies iterables whose
    chunks are not bytes.
  • Reading a buffered body twice returns the cached bytes; workerd throws
    Body has already been used....
  • cache.put() over a string-chunk body still reaches the RangeError path
    in cache.js's own capped drain.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 21 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4fb0c136-ac44-4f5b-b7ee-a192bbbbfd7a
Commits

Reviewing files that changed from the base of the PR and between f4864e1 and 04b1086.

Files selected for processing: 5

  • crates/cello/js/cache.js
  • crates/cello/js/harness.js
  • docs/changelog.md
  • scripts/workerd-diff/cases.mjs
  • scripts/workerd-diff/corpus/streams/index.js
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@ryuzdev
ryuzdev force-pushed the fix/workerd-diff-bodies branch from 48ef12d to c0cb91a Compare November 22, 2026 16:52
- bodyUsed is the Fetch observation again: a Response/Request body stream that
  is disturbed (a read or cancel through any reader) reports true mid-stream,
  so a guard re-checking it is no longer bypassed (#115). The prototype
  accessor is getter-only, as workerd's readonly property is; runtime code
  marks the explicit half (_bodyUsed) through __markBodyUsed: buffered-body
  consumers, host-stream transfers, subrequest forwards and cache.put().
- clone() of a consumed, disturbed or locked body throws workerd's
  TypeError("This ReadableStream is currently locked to a reader,") instead of
  "Body has already been consumed" (#116). The gate stays stricter than
  workerd's after a manual read plus releaseLock(), which workerd lets through
  with empty bodies.
- A body stream chunk that is not bytes, a view or an ArrayBuffer now rejects
  every consumer with TypeError("This ReadableStream did not return bytes.")
  before any bytes are returned, instead of a RangeError from the byte
  arithmetic (#117). A chunk's window is read from internal slots through the
  captured %TypedArray%/DataView prototype getters, and the chunk is
  classified by its prototype chain, so own buffer/byteOffset/byteLength
  properties a guest defines on a view cannot pad the body, read another
  window, or throw.
- Request.prototype.bytes() and Response.prototype.bytes() exist, so bytes() is
  covered like the other consumers.
- workerd-diff: six new agreeing cases (body-used-reader, res-body-used,
  clone-consumed, string-chunks, string-chunks-response, body-chunk-shadows);
  31/31 agree.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant