← Back to Pull requests

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

Closed
unknown closed this pull request without merging on Nov 27, 2026 23:53 UTC
View on GitHub
unknown unknown commented on Nov 26, 2026 06:06 UTC
Author

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

    Diff: 595 lines (+446 / -149) across 7 files Reviewer: jasnell Review duration: 2507m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 595 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8938"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50% (MEDIUM) · Higher = larger blast radius
    50%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.8% · Safe to merge
    19.8%