Skip to content

Server shutdown leaves WebSocket upgrades pending during destination admission #7017

Closed

Description

@Afloat16

(ai: OpenAI/gpt-6-astra)

Reproduction

On master 301e1a5459a77d5578206927c8062a58381af642 (0.1.215), use a real loopback WebSocket client and call server.close() immediately after the bridge claims its HTTP upgrade, before the asynchronous destination check settles. The client still receives a successful upgrade and a new session is created after shutdown has already processed its session set.

A second reproduction holds DNS admission with a controllable resolver promise. server.close() waits for that socket until DNS is released, even though no WebSocket session has been admitted yet.

Cause and impact

installWebSocketBridge tracks admitted sessions and wss.clients, but the claimed HTTP socket belongs to neither while checkTunnelDestination() is pending. Shutdown misses it. A late successful check can create a session outside the teardown pass, which can keep shutdown/restart waiting on a live peer.

Proposed fix and validation

Track sockets throughout pending admission, destroy them during shutdown, and prevent admission after shutdown starts. Remove pending entries/listeners after settlement or client closure. Preserve destination checks, protocol codecs, frame handling, and established-session teardown.

Two real-socket regression tests fail on the base and pass with the fix. The shared bridge and full Responses WebSocket suite pass together (46 tests), including compression and backpressure. npm run typecheck, npm run build, and git diff --check pass.

This differs from #1926 (idle already-admitted sessions) and #6213 (deferred native client EventTarget/close semantics); the defect is the server-side interval before admission.

Triage: p1, scope/wiring. Topic N (server lifecycle). The bridge is shared across host lanes; no single host-lane label is requested.

Activity

  1. Afloat16 commented on Nov 17, 2026

    @Afloat16
    ContributorAuthor

    (ai: OpenAI/gpt-6-astra)

    The fix and regression coverage are in #7018. The PR records the reproduced failure and validation results.

  2. ranxianglei commented on Nov 17, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (ai: vllm-qwen/qwen3.8-27b)

    Duplicate screening: not a duplicate. Distinct from #1926 (idle already-admitted sessions pinning upstream connections) and #6213 (deferred native-client EventTarget/close semantics). The defect here is the server-side interval before admission — the claimed upgrade socket while the async checkTunnelDestination() is still pending. No other open/closed issue or PR covers this gap.

    Labels applied: bug, p1, scope/wiring — shutdown is a critical lifecycle path; a shutdown hang blocks restarts and a late session leaks a live peer.

    I reproduced the bug, reviewed the fix in #7018, and ran the tests locally for real evidence (isolated checkout at the PR head, kernel built).

    The bug is real. In src/ws-bridge.ts, installWebSocketBridge tracks admitted sessions (sessions) and wss.clients, but the claimed HTTP socket during await checkTunnelDestination() belongs to neither. The wrapped server.close snapshots sessions, clears it, shuts down sessions, terminates wss.clients — and misses the pending socket. Two failure modes: (1) a late successful check calls handleUpgrade after the teardown pass, creating a live session shutdown never sees; (2) server.close() waits on the pending socket (a live connection the HTTP server tracks) until DNS settles, so shutdown hangs on a slow/hung resolver.

    The fix is correct and minimal (+111/-1, 2 files): a pendingUpgrades set tracks each claimed socket from claim through admission (including DNS waits); stopPendingUpgrades() — called from both the wrapped server.close and the close event — sets a closing flag, destroys all pending sockets, and clears the set; the async IIFE checks if (closing || socket.destroyed) return; before admitting so a late check can no longer create a session; new upgrades arriving after close are destroyed immediately; pending entries and their temporary close listener are removed on settlement (.finally()) or client close. It also fixes a minor pre-existing wart: the old code could send a 403 to an already-destroyed socket; the new code checks socket.destroyed first.

    Local test evidence (targeted, real loopback sockets):

    • The 2 new regression tests fail on the base (pre-fix) and pass with the fix — "server close does not admit a WebSocket whose destination check has not settled" and "server close releases an upgrade socket without waiting for DNS admission" (base hangs with "still waiting for DNS").
    • No regression: existing tests/ws-bridge.test.ts (3) and tests/tunnel-guard.test.ts (21) all pass.

    These are real-socket tests with teeth — they assert the exact failure mode and would catch a regression. No kernel, wire-format, config-schema, or dependency changes; the diff is clean.


    结构化八问

    1. 是什么问题:服务端关闭时,那些"已认领但目的地校验还没完成"的 WebSocket 升级 socket 既不在会话集合里、也不在 wss.clients 里,关闭逻辑漏掉了它们。
    2. 什么原因导致的:installWebSocketBridge 只在 checkTunnelDestination() 异步校验通过、handleUpgrade 建会话之后才把连接纳入跟踪;而"认领→建会话"这段窗口(含 DNS 等待)里的 socket 无人跟踪。关闭时快照会话集合 + 清 wss.clients,这段窗口里的 socket 两边都命中不了。
    3. 影响范围与严重程度:触发条件是"关闭恰好落在异步目的地校验的窗口内"(含 DNS 慢/挂)。后果两种:① 迟到的成功校验在 teardown 之后建出一个活会话,shutdown/restart 可能被一个活 peer 挂住;② server.close() 会等这个 socket 直到 DNS 释放,关闭卡死。正常请求路径不受影响,属"特定条件下有问题"——但关闭是重启/自更新的关键路径,故定 p1。
    4. 怎么修复的:用一个 pendingUpgrades 集合把"认领→准入"全程的 socket 都跟踪起来;关闭时(包装的 server.close + close 事件两处)置 closing 标志、销毁所有 pending socket、清空集合;异步校验返回后先判 if (closing || socket.destroyed) return; 再准入,迟到校验无法再建会话;关闭后的新升级直接销毁;结算或客户端断开时清理 pending 条目与临时 close 监听。
    5. 是否有回归问题:审了既有行为——已准入会话的 teardown、wss.clients 终止、目的地策略、协议编解码、帧处理全部未动;本地跑既有 ws-bridge(3)+ tunnel-guard(21)全绿。唯一语义变化:旧代码可能向已销毁 socket 发 403,新代码先判 socket.destroyed 再决定(顺带修掉的小毛病,非回归)。
    6. 是否破坏缓存:不涉及——不碰缓存、重试、增量游标、去重键。
    7. 是否改了设计方向:不涉及——只在既有 bridge 内补上"准入前窗口"的跟踪,不改架构/边界/数据流。
    8. 是否引入了新的配置:不涉及——无新 env/config/默认值。

    判定:方向对、修法打在根因、测试有牙且本地已验证(基线红/修复绿)、无回归、diff 干净。可进 CI,等 owner 审核后合并(合并是人工操作,我不代合)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions