Repository navigation
Let hang detection see TS-streams deadlocks with in-isolate JS - #8938
Merged
Merged
Conversation
The runtime fails a request that nothing can advance any more ("your
Worker's code had hung and would never generate a response"). It
judges that by the pending events the request holds, so waiting on
something that only JavaScript in the same isolate can complete must
not hold one. The C++ streams implementation reads and writes streams
with awaitIoLegacy() for this reason: the other end of a stream may
be JavaScript, such as an IdentityTransformStream's writer or
HTMLRewriter's handlers. Streams backed by external I/O hold their
own pending events while it is in flight (system-streams.c++).
Under typescript_implemented_streams, the bridges to native streams
waited with awaitIo(), which holds a pending event: the queued pump's
writes to its sink, body collection from a native source, and native
sources' pulls. A Worker whose HTMLRewriter handler waits on a write
that only the rewriter can consume therefore hung forever instead of
failing.
Add IoContext::awaitIoWithoutPendingEvent(), awaitIo() without the
pending event, and use it at those three places, matching the C++
implementation. The new test deadlocks an HTMLRewriter handler this
way, consuming the output with arrayBuffer() and with a reader, and
expects the request to fail, under both implementations. Under
TypeScript streams it hung before this change.
js-readable-stream-test's StallingSink stands in for a sink backed by
external I/O, so it now holds a pending event while a write is
outstanding, as such sinks do. Without one, the IoContext rightly
took its stalled write for a hang.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jasnell
approved these changes
Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Under
typescript_implemented_streams, a Worker that deadlocks itself through a stream hangs forever. Under the C++ implementation, the same Worker fails with "...your Worker's code had hung and would never generate a response". One example: an HTMLRewriter element handler that awaits a write to the rewriter's own input, which the rewriter can't read until the handler returns.Cause
Hang detection fails a request that holds no pending events and has nothing else left to run. Waiting on something that only JavaScript in the same isolate can complete must therefore not hold a pending event. The C++ streams implementation reads and writes streams with
awaitIoLegacy()for this reason (see the TODOs ininternal.c++): the other end of a stream may be JavaScript, such as anIdentityTransformStream's writer or HTMLRewriter's handlers. Streams backed by external I/O hold their own pending events while that I/O is in flight (system-streams.c++).The TypeScript implementation's bridges to native streams waited with
awaitIo(), which holds a pending event, in three places:pumpQueuedTsStream()), which feeds HTMLRewriter's input;arrayBuffer(),text()and friends), which reads HTMLRewriter's output;ReadableStreamNativeSource's default and BYOB pulls, which read HTMLRewriter's output through a reader.Fix
Add
IoContext::awaitIoWithoutPendingEvent(), which isawaitIo()minus the pending event, and use it in those three places. That matches the C++ implementation's waits. I added a new name rather than reusingawaitIoLegacy(), which is deprecated because it lacks a continuation function. End/close and native pipes still hold pending events, as their C++ counterparts do.Test
htmlrewriter-deadlock-testsets up that deadlock and consumes the rewriter's output two ways, witharrayBuffer()and with a reader. It expects the request to fail with the hang error, under both implementations. Before this change both TypeScript cases hung until the test timed out. Reverting any one of the three sites hangs a case again.Checked on bazelflare:
htmlrewriter-test,http-test,http-standard-test,cache-test,streams/streams-test,streams/body-read-operation-test,streams-error-edge-cases-test,streams-terminal-state-test,pipe-source-error-uaf-test,js-readable-stream-testandjs-writable-stream-testpass in their default variants, and the wd-tests among them also pass in@all-compat-flags.Known remaining TypeScript-streams failures
I added
typescript_implemented_streamsto the configs of the tests above, plus the socket and node tls/net tests, and ran them on main and on this branch. Both runs fail the same cases, except forhtmlrewriter-test:htmlrewriter-testmanualWriting2,streamingReplacement,streamingReplacementBadUTF8,streamingReplacementHTMLmanualWriting2hangs until the test times out, so the cases after it never run. With this PR all four fail at once with the hang error. They awaitTransformStreamwrites before reading, which depends onoriginal-transform-stream-backpressure. The test config sets that flag, but the TypeScript implementation doesn't support it.http-testhttp-test:test,http-test-cache-option-enabled:test,cacheModehttp-socket-testsocketFetcherUnusablestreams-terminal-state-testbufferedClosePreservesLifecycle,bufferedErrorPreservesLifecycle,terminalSettlementPreservesLifecycle,cachedByobResponseDuringClosenestedError);Cannot read properties of null (reading 'view')streams/body-read-operation-testbodyReadUsesOperationLocalBytes,callbacksRetainPendingBodyRead,pendingBodyReadRequiresCompletion,uploadAbortDuringReasonConversionnode/tests:tls-nodejs-testPass with the flag on:
cache-test,http-standard-test,streams/streams-test,streams-error-edge-cases-test,pipe-source-error-uaf-test,connect-handler-test,connect-handler-starttls-lifetime-test,js-rpc-socket-test,socket-close-gc-test,socket-proxy-to-test,sockets-connect-error-test,outbound-interceptor-socket-test,starttls-nodejs-test,net-nodejs-test.The companion fix for sockets upgraded with
startTls()is in #8937.🤖 Generated with Claude Code