← Back to Pull requests

Close the connection when an upgraded socket closes, on TS streams #8937

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

Under typescript_implemented_streams, closing the socket that startTls() returned leaves the connection open. The peer never sees it end, so a peer that waits for EOF hangs.

Cause

startTls() and takeConnectionStream() neutralize the old socket's readable by detaching it, and drop the detached copy. Under the C++ streams implementation, dropping the copy frees it at once, and with it its source's reference to the connection. Under TypeScript streams the copy is a JavaScript object, and its native source holds the connection until that object is garbage-collected. The writable side already handles this: WritableStreamNativeSink::detach() releases its sink eagerly.

Fix

Socket::detachStreams() now cancels the detached readable instead of dropping it, which releases its source right away under both implementations. The cancellation reaches only the old socket's own reads (EncodedAsyncInputStream::cancel() cancels the reads it has pending), not the connection, which the new socket now owns.

Test

connect-handler-starttls-close-test opens a starttls tunnel to a Durable Object's connect() handler. Both ends upgrade, the test closes its upgraded socket, and the handler reports through RPC how its pending read ended. The test runs the same worker under both implementations. Both now report the same disconnect error. Before this change the TypeScript run hung until the test timed out.

Checked on bazelflare:

  • The new test passes 5 times in each of its three variants (@, @all-autogates, @all-compat-flags).

  • These socket tests pass in @ and @all-compat-flags: connect-handler-test, connect-handler-starttls-lifetime-test, http-socket-test, socket-close-gc-test, socket-proxy-to-test, sockets-connect-error-test, outbound-interceptor-socket-test, js-rpc-socket-test, starttls-nodejs-test, tls-nodejs-test, net-nodejs-test, and sockets-test.

  • With typescript_implemented_streams added to their configs, the same tests fail exactly as they do on main: http-socket-test:socketFetcherUnusable and six tls-nodejs-test cases ("The strategy for a byte stream cannot have a size function"). Those are separate TypeScript-streams gaps, listed in #8938.
  • Not covered

    When a connect() handler closes its upgraded socket, the caller still sees no EOF until the handler returns. This happens under both implementations, so it's a different issue and this PR leaves it alone.

    🤖 Generated with Claude Code

    Diff: 315 lines (+236 / -79) across 3 files Reviewer: jasnell Review duration: 2509m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 315 lines diff across 3 files
    0.40
    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.344
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8937"
    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: 40.4% (MEDIUM) · Higher = larger blast radius
    40.4%
    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: 15.9% · Safe to merge
    15.9%