Skip to content

Let hang detection see TS-streams deadlocks with in-isolate JS - #8938

Merged
danlapid merged 1 commit into
mainfrom
dlapid/ts-streams-hang-detection
Nov 27, 2026
Merged

danlapid merged 1 commit into
mainfrom
dlapid/ts-streams-hang-detection

Conversation

@danlapid

Copy link
Copy Markdown
Collaborator

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 in internal.c++): 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 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:

  • the queued pump's writes to its sink (pumpQueuedTsStream()), which feeds HTMLRewriter's input;
  • body collection from a native source (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 is awaitIo() 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 reusing awaitIoLegacy(), 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-test sets up that deadlock and consumes the rewriter's output two ways, with arrayBuffer() 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:

  • The new test passes 5 times in each of its three variants.
  • 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-test and js-writable-stream-test pass 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_streams to 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 for htmlrewriter-test:

Test Failing cases First error
htmlrewriter-test manualWriting2, streamingReplacement, streamingReplacementBadUTF8, streamingReplacementHTML On main, manualWriting2 hangs 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 await TransformStream writes before reading, which depends on original-transform-stream-backpressure. The test config sets that flag, but the TypeScript implementation doesn't support it.
http-test http-test:test, http-test-cache-option-enabled:test, cacheMode Value assertions
http-socket-test socketFetcherUnusable Deep-equal assertion
streams-terminal-state-test bufferedClosePreservesLifecycle, bufferedErrorPreservesLifecycle, terminalSettlementPreservesLifecycle, cachedByobResponseDuringClose Lifecycle records differ (e.g. nestedError); Cannot read properties of null (reading 'view')
streams/body-read-operation-test bodyReadUsesOperationLocalBytes, callbacksRetainPendingBodyRead, pendingBodyReadRequiresCompletion, uploadAbortDuringReasonConversion "Expected a rejection"; hang error; "adoption failed"; "upload failed"
node/tests:tls-nodejs-test 6 cases "The strategy for a byte stream cannot have a size function"

Pass 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

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>
@danlapid
danlapid marked this pull request as ready for review November 27, 2026 23:52
@danlapid
danlapid requested review from a team as code owners November 27, 2026 23:52
@danlapid
danlapid merged commit 86fc7b9 into main Nov 27, 2026
23 of 24 checks passed
@danlapid
danlapid deleted the dlapid/ts-streams-hang-detection branch November 27, 2026 23:53
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.

2 participants