Repository navigation
Conversation
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 detectedLatest commit: e0e3b6a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
commit: |
There was a problem hiding this comment.
✅ 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
🚀 Deploying Preview to Cloudflare 🚀Preview URL: https://fix-refuse-locked-or-resent-stream.pr.capnweb.com (commit e0e3b6a)This URL reflects your latest Preview deploymentPreview Deployments by commit
|
Sending a
ReadableStreamthat 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 thepipeTo()rejection. The serializer now throws.Fixes #295. Also closes the gap #296 opened, where
Promise.all([p, p.stream])stopped rejecting.RpcPromiseResponsetwiceRequest/Responsewhose body was already readPromise.all([p, p.stream])Measured on Node and workerd, direct and through a forwarding session. Results with this PR are the same on both.
lockedalone is not enough. A drained stream is released, so a second send sees it unlocked and delivers nothing. Hence aWeakSetof streams already sent.pipeTo()has started, after which the stream can never be sent again, so it can't couple unrelated payloads.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.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.RpcPromisewith such a stream is now an unhandled rejection on Node, as any unserializable argument already is.Requestbodies, which take thearrayBuffer()fallback and never reach the check (from reading the code, not run), and aRequestalready copied withnew Request(req)on Node, whose old body ends up unlocked. The changeset claimsReadableStreamonly.tee()halves, a released reader, a received stream relayed on,clone(), aBlobsent twice, anErrorcarrying a locked stream (property dropped, as before).Promise.allSettled([p, p.stream]), which also checks the result's own stream still reads. All fail on main. Dropping thelockedcheck, the sent check, or the recording line each fails one, on Node and workerd.rpc.tschange applied. Browser projects not run locally.