Both stream adapters waited for the source's pending next() after the client had disconnected. A source idle between events (an SSE feed with nothing to send) may never settle it:
Node (pipeStream): the loop never reached its finally, so stream:close did not fire and the closer stayed in the stream registry that graceful shutdown walks.
Fetch (asReadableStream): cancel awaited return(), which an async generator only runs once its pending next() settles, so cancelling the body did not settle. With compression, compressWebStream.cancel awaits that cancel, so it hung too.
A return() that throws synchronously escaped the Node close listener as an uncaught exception, and rejected cancel() on the fetch path.
Fix. Each pull is raced against the disconnect (the response's close on Node, the stream's cancel on fetch). The source's return() is called without being waited on, through a small internal releaseIterator in src/channel.ts that also guards a synchronous throw. That is the idea the WebSocket abort listener in ws-core.ts already used, and it now calls the same helper.
Test.test/stream-lifecycle-parity.test.ts gains a matrix: Node and fetch, with and without compression, against an async generator, a return() that never settles, and a return() that throws. On develop 11 of 12 fail (the uncompressed fetch + generator cell already passed); all pass with the fix.
No Deno/Bun/edge cell added: none of those suites has a disconnect test to extend. They run the same asReadableStream code, but how each runtime calls cancel on a dropped client is not asserted there.
pumpOutbound (WebSocket) still awaits next() without a race. Its closer is unregistered on abort, so shutdown is not held up, but stream:close for an idle ws source still waits for the next value. Left alone to keep this change scoped.
An async generator suspended mid-await still runs its finally only when that await settles. The language gives no way to interrupt it; the fix only stops the adapters from waiting on it.
Fixes GitHub #40.
Both stream adapters waited for the source's pending `next()` after the client had disconnected. A source idle between events (an SSE feed with nothing to send) may never settle it:
- **Node** (`pipeStream`): the loop never reached its `finally`, so `stream:close` did not fire and the closer stayed in the stream registry that graceful shutdown walks.
- **Fetch** (`asReadableStream`): `cancel` awaited `return()`, which an async generator only runs once its pending `next()` settles, so cancelling the body did not settle. With compression, `compressWebStream.cancel` awaits that cancel, so it hung too.
- A `return()` that throws synchronously escaped the Node `close` listener as an uncaught exception, and rejected `cancel()` on the fetch path.
**Fix.** Each pull is raced against the disconnect (the response's `close` on Node, the stream's `cancel` on fetch). The source's `return()` is called without being waited on, through a small internal `releaseIterator` in `src/channel.ts` that also guards a synchronous throw. That is the idea the WebSocket abort listener in `ws-core.ts` already used, and it now calls the same helper.
**Test.** `test/stream-lifecycle-parity.test.ts` gains a matrix: Node and fetch, with and without compression, against an async generator, a `return()` that never settles, and a `return()` that throws. On develop 11 of 12 fail (the uncompressed fetch + generator cell already passed); all pass with the fix.
**Verification.** lint (0 errors), format:check, typecheck, complexity:check, `npm test` 1172/1172, test:deno 63/63, test:bun 52/52, test:edge PASS.
**Unsure / not covered.**
- No Deno/Bun/edge cell added: none of those suites has a disconnect test to extend. They run the same `asReadableStream` code, but how each runtime calls `cancel` on a dropped client is not asserted there.
- `pumpOutbound` (WebSocket) still awaits `next()` without a race. Its closer is unregistered on abort, so shutdown is not held up, but `stream:close` for an idle ws source still waits for the next value. Left alone to keep this change scoped.
- An async generator suspended mid-`await` still runs its `finally` only when that await settles. The language gives no way to interrupt it; the fix only stops the adapters from waiting on it.
Both stream adapters waited for the source's pending next() after the
client had disconnected. A source idle between events, such as an SSE
feed with nothing to send, may never settle that next(), so on Node
pipeStream never reached its finally: stream:close did not fire and the
closer stayed registered, which graceful shutdown then still treated as
a live stream. Through app.fetch, the ReadableStream's cancel awaited
return(), which an async generator only runs once its pending next()
settles, so cancelling the body did not settle either, with or without
compression wrapping it.
Each pull is now raced against the disconnect: the response's close
event on Node, and the stream's cancel on the fetch path. The source's
return() is called without being waited on, through a small
releaseIterator helper that also guards a return() that throws
synchronously, which on Node escaped from the close listener as an
uncaught exception. The WebSocket abort listener already released its
iterator this way and now uses the same helper.
The new cases in stream-lifecycle-parity drive both adapters, with and
without compression, against an async generator, a return() that never
settles and a return() that throws. All twelve need the fix except the
uncompressed fetch case with a generator, which already passed.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Fixes GitHub #40.
Both stream adapters waited for the source's pending
next()after the client had disconnected. A source idle between events (an SSE feed with nothing to send) may never settle it:pipeStream): the loop never reached itsfinally, sostream:closedid not fire and the closer stayed in the stream registry that graceful shutdown walks.asReadableStream):cancelawaitedreturn(), which an async generator only runs once its pendingnext()settles, so cancelling the body did not settle. With compression,compressWebStream.cancelawaits that cancel, so it hung too.return()that throws synchronously escaped the Nodecloselistener as an uncaught exception, and rejectedcancel()on the fetch path.Fix. Each pull is raced against the disconnect (the response's
closeon Node, the stream'scancelon fetch). The source'sreturn()is called without being waited on, through a small internalreleaseIteratorinsrc/channel.tsthat also guards a synchronous throw. That is the idea the WebSocket abort listener inws-core.tsalready used, and it now calls the same helper.Test.
test/stream-lifecycle-parity.test.tsgains a matrix: Node and fetch, with and without compression, against an async generator, areturn()that never settles, and areturn()that throws. On develop 11 of 12 fail (the uncompressed fetch + generator cell already passed); all pass with the fix.Verification. lint (0 errors), format:check, typecheck, complexity:check,
npm test1172/1172, test:deno 63/63, test:bun 52/52, test:edge PASS.Unsure / not covered.
asReadableStreamcode, but how each runtime callscancelon a dropped client is not asserted there.pumpOutbound(WebSocket) still awaitsnext()without a race. Its closer is unregistered on abort, so shutdown is not held up, butstream:closefor an idle ws source still waits for the next value. Left alone to keep this change scoped.awaitstill runs itsfinallyonly when that await settles. The language gives no way to interrupt it; the fix only stops the adapters from waiting on it.bdb2f8c7ffto1daa4f3cd1