You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Server shutdown leaves WebSocket upgrades pending during destination admission #7017
On master301e1a5459a77d5578206927c8062a58381af642 (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.
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 handleUpgradeafter 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.
Activity
Afloat16 commentedon Nov 17, 2026
(ai: OpenAI/gpt-6-astra)
The fix and regression coverage are in #7018. The PR records the reproduced failure and validation results.
ranxianglei commentedon Nov 17, 2026
(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,installWebSocketBridgetracks admitted sessions (sessions) andwss.clients, but the claimed HTTP socket duringawait checkTunnelDestination()belongs to neither. The wrappedserver.closesnapshotssessions, clears it, shuts down sessions, terminateswss.clients— and misses the pending socket. Two failure modes: (1) a late successful check callshandleUpgradeafter 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
pendingUpgradesset tracks each claimed socket from claim through admission (including DNS waits);stopPendingUpgrades()— called from both the wrappedserver.closeand thecloseevent — sets aclosingflag, destroys all pending sockets, and clears the set; the async IIFE checksif (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 temporarycloselistener 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 checkssocket.destroyedfirst.Local test evidence (targeted, real loopback sockets):
tests/ws-bridge.test.ts(3) andtests/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.
结构化八问
wss.clients里,关闭逻辑漏掉了它们。installWebSocketBridge只在checkTunnelDestination()异步校验通过、handleUpgrade建会话之后才把连接纳入跟踪;而"认领→建会话"这段窗口(含 DNS 等待)里的 socket 无人跟踪。关闭时快照会话集合 + 清wss.clients,这段窗口里的 socket 两边都命中不了。server.close()会等这个 socket 直到 DNS 释放,关闭卡死。正常请求路径不受影响,属"特定条件下有问题"——但关闭是重启/自更新的关键路径,故定 p1。pendingUpgrades集合把"认领→准入"全程的 socket 都跟踪起来;关闭时(包装的server.close+close事件两处)置closing标志、销毁所有 pending socket、清空集合;异步校验返回后先判if (closing || socket.destroyed) return;再准入,迟到校验无法再建会话;关闭后的新升级直接销毁;结算或客户端断开时清理 pending 条目与临时 close 监听。wss.clients终止、目的地策略、协议编解码、帧处理全部未动;本地跑既有ws-bridge(3)+tunnel-guard(21)全绿。唯一语义变化:旧代码可能向已销毁 socket 发 403,新代码先判socket.destroyed再决定(顺带修掉的小毛病,非回归)。判定:方向对、修法打在根因、测试有牙且本地已验证(基线红/修复绿)、无回归、diff 干净。可进 CI,等 owner 审核后合并(合并是人工操作,我不代合)。