Skip to content

fix: refuse to send a ReadableStream that is locked or was already sent - #304

Open
ndisidore wants to merge 1 commit into
mainfrom
fix/refuse-locked-or-resent-stream
Open

ndisidore wants to merge 1 commit into
mainfrom
fix/refuse-locked-or-resent-stream

Conversation

@ndisidore

Copy link
Copy Markdown
Member

Sending a ReadableStream that is locked, or sending the same one twice, went through with no error. The pipe got nothing, so the receiver's stream hung, or ended at once if the first pipe had already finished. createPipe() swallows the pipeTo() rejection. The serializer now throws.

Fixes #295. Also closes the gap #296 opened, where Promise.all([p, p.stream]) stopped rejecting.

Case main This PR
Locked stream as an argument Receiver hangs Throws at the call site
Same stream sent twice Second receiver hangs or gets nothing Throws at the call site
Either, in a call queued on a pending RpcPromise Hangs or empty Promise rejects
Server returns a cached stream or Response twice Second caller hangs or gets an empty body Second call rejects
Request/Response whose body was already read Hangs Throws or rejects
Promise.all([p, p.stream]) Second stream hangs (Node) or is empty (workerd) Rejects

Measured on Node and workerd, direct and through a forwarding session. Results with this PR are the same on both.

  • Throw in the serializer, before a hook or pipe exists for that stream. Sender gets it at the call site; for a result, the caller's promise rejects and the session carries on. The first option in Sending a locked ReadableStream hangs the receiver instead of erroring #295.
  • locked alone is not enough. A drained stream is released, so a second send sees it unlocked and delivers nothing. Hence a WeakSet of streams already sent.
  • Process-wide, not per session. The second send may be on another session. Membership means pipeTo() has started, after which the stream can never be sent again, so it can't couple unrelated payloads.
  • Not the second option in Sending a locked ReadableStream hangs the receiver instead of erroring #295 (abort the writable when pipeTo() rejects). It wouldn't catch the drained case. A chunk that fails to serialize mid-pump still hangs the receiver, as on main. Left for a follow-up.
  • Known cost. Streams earlier in the same message already have pipes when the throw happens, and nothing rolls them back (the TODO in devaluate()). stub.put(freshStream, reusedRequest) strands three table entries per refused call until the session ends; main went through with an empty body and left none. Reused one first: nothing stranded. The fix is pipe rollback, a follow-up.
  • An un-awaited call queued on a pending RpcPromise with such a stream is now an unhandled rejection on Node, as any unserializable argument already is.
  • Not covered, both as on main: Firefox Request bodies, which take the arrayBuffer() fallback and never reach the check (from reading the code, not run), and a Request already copied with new Request(req) on Node, whose old body ends up unlocked. The changeset claims ReadableStream only.
  • Unchanged: fresh streams, tee() halves, a released reader, a received stream relayed on, clone(), a Blob sent twice, an Error carrying a locked stream (property dropped, as before).
  • Tests go through the public API: a locked argument (the Sending a locked ReadableStream hangs the receiver instead of erroring #295 repro), the same stream sent again on a second session after the first send drained it, and Promise.allSettled([p, p.stream]), which also checks the result's own stream still reads. All fail on main. Dropping the locked check, the sent check, or the recording line each fails one, on Node and workerd.
  • Ran the Node and workerd projects locally, also with fix: release call arguments once a forwarded or queued call is sent #294's rpc.ts change applied. Browser projects not run locally.

A ReadableStream can feed only one pipe. Sending one that is locked, or
sending the same one twice, still created a pipe, and pipeTo() then
failed or found nothing left. createPipe() swallows that, so the call
went through and the receiver got a stream that hung, or ended at once
if the first pipe had already finished. Neither side saw an error.

Throw from the serializer instead, before a hook or pipe is made for
that stream. The sender gets the error at the call site; when a result
is being serialized, the caller's promise rejects and the session
carries on.

`locked` alone is not enough. Once the first pipe drains the stream it
is released, so a later send sees an unlocked stream and delivers
nothing. Hence the record of streams already sent, kept per process
because the second send may be on another session.

A Response body goes through the same case, so a cached Response
returned twice now rejects the second call. So does a Request body
where the platform exposes one as a stream and keeps it locked once
read. Firefox, and a Request copied with `new Request(req)` on Node,
are not covered.

This also closes the gap #296 opened: `Promise.all([p, p.stream])`
resolved with a second stream that hung on Node and was empty on
workerd. It rejects again.

Known cost: streams serialized earlier in the same message already
have pipes when the throw happens, and nothing rolls those back (the
TODO in devaluate()). A fresh stream placed ahead of a refused one is
left unread and holds table entries until the session ends, where the
same call used to go through with an empty stream and leave nothing.

Fixes #295.
@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e0e3b6a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
capnweb Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/capnweb@304
npm i https://pkg.pr.new/cloudflare/capnweb/capnweb-validate@304

commit: e0e3b6a

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — The fix is small and correct: the check runs before a pipe is created, the stream is recorded only after the pipe starts, and tests cover each case. Nothing here blocks merging.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


I found no problems in this pull request. The serializer refuses a ReadableStream that is locked or was already sent, and it does so before any hook or pipe exists for that stream. The stream is recorded as sent only after createPipe() succeeds, so a failed attempt leaves nothing behind. The new tests use the public API and cover the locked-argument repro, a drained stream sent again on a second session, and the Promise.all([p, p.stream]) case.

One optional follow-up: the streaming concepts page (packages/docs/src/content/docs/concepts/streaming.mdx) doesn't yet say that a stream can be sent only once and must be unlocked. Users may hit the new TypeError without knowing why, so a sentence there would help.


Reviewed commit: e0e3b6a4

@cloudflare-workers-and-pages

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-refuse-locked-or-resent-stream.pr.capnweb.com (commit e0e3b6a)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://39c344ee.pr.capnweb.com e0e3b6a 2026-11-18T16:50:30.209Z Visit the dashboard ↗

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sending a locked ReadableStream hangs the receiver instead of erroring

1 participant