Repository navigation
fix(mobile): harden WebSocket accept path against socket overload - #9247
Merged
Merged
Conversation
…-healing The desktop runtime's mobile WebSocket server on :6768 could wedge with the socket in LISTEN but the TCP accept loop stalled — new connections piling up in SYN_RCVD with zero ESTABLISHED, even from localhost, with no self-heal. Since Orca mobile has no APNs (the persistent WS doubles as the push channel), a wedged accept loop silently drops every notification while the app still shows cached UI. Root cause: connections were only capped at the WebSocket-upgrade layer (MAX_WS_CONNECTIONS), after the socket is already accepted. TCP-level sockets were unbounded, so leaked/half-open sockets (backgrounded phones, flaky relay, reconnect storms) could grow until the process ran out of file descriptors and accept() started failing with EMFILE. - Bound TCP sockets via httpServer.maxConnections (2x the WS cap). At the cap Node accepts-then-closes, so the accept loop always keeps draining and the EMFILE/SYN_RCVD wedge is structurally impossible on this listener. - Handle accept-level errors: ws forwards httpServer 'error' onto the WebSocketServer; with no listener Node rethrows it as an uncaught exception. Swallow + log and keep listening. - Force-terminate over-capacity sockets: a bare ws.close(1013) left half-open phones lingering at >cap forever, rejecting everyone after them. Add an error handler + a 1s terminate fallback so the descriptor is always freed. - Log heartbeat reaping / near-cap live counts so the leak is observable.
Contributor
📝 WalkthroughWalkthroughThe WebSocket transport now applies a TCP connection limit, routes over-capacity connections through graceful close and forced termination, and tracks heartbeat-reaped sockets. Heartbeat processing emits warnings when sockets are reaped or the WebSocket capacity threshold is reached. Tests cover the TCP limit and forced termination behavior. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Contributor
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 84b9b38a-5502-448e-bb9b-e0fc7acb68dd
📒 Files selected for processing (2)
src/main/runtime/rpc/ws-transport.test.tssrc/main/runtime/rpc/ws-transport.ts
enrick-dev
pushed a commit
to enrick-dev/orca
that referenced
this pull request
Oct 15, 2026
…ablyai#9247) * fix(mobile): make the :6768 WebSocket accept loop leak-proof and self-healing The desktop runtime's mobile WebSocket server on :6768 could wedge with the socket in LISTEN but the TCP accept loop stalled — new connections piling up in SYN_RCVD with zero ESTABLISHED, even from localhost, with no self-heal. Since Orca mobile has no APNs (the persistent WS doubles as the push channel), a wedged accept loop silently drops every notification while the app still shows cached UI. Root cause: connections were only capped at the WebSocket-upgrade layer (MAX_WS_CONNECTIONS), after the socket is already accepted. TCP-level sockets were unbounded, so leaked/half-open sockets (backgrounded phones, flaky relay, reconnect storms) could grow until the process ran out of file descriptors and accept() started failing with EMFILE. - Bound TCP sockets via httpServer.maxConnections (2x the WS cap). At the cap Node accepts-then-closes, so the accept loop always keeps draining and the EMFILE/SYN_RCVD wedge is structurally impossible on this listener. - Handle accept-level errors: ws forwards httpServer 'error' onto the WebSocketServer; with no listener Node rethrows it as an uncaught exception. Swallow + log and keep listening. - Force-terminate over-capacity sockets: a bare ws.close(1013) left half-open phones lingering at >cap forever, rejecting everyone after them. Add an error handler + a 1s terminate fallback so the descriptor is always freed. - Log heartbeat reaping / near-cap live counts so the leak is observable. * fix(mobile): avoid masking fatal accept failures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The desktop mobile WebSocket listener could remain in
LISTENwhile new TCP handshakes stopped completing, disconnecting the persistent channel used for mobile RPC and notifications.This change hardens the listener against connection accumulation:
1013close, then force-terminates them after one second so reconnect floods cannot retain rejected sockets until the next 15-second heartbeat.Screenshots
No visual change.
Testing
pnpm lint— changed files pass; the repository-wide command reaches two unrelated existing switch-exhaustiveness errors innative-chat-session-option-labels.tsandskill-freshness-group.tsx.pnpm typecheckpnpm test— focused transport and full runtime RPC suites were run instead.pnpm build— not needed for this isolated main-process transport change.Executed locally:
pnpm exec vitest run --config config/vitest.config.ts src/main/runtime/rpc/ws-transport.test.ts— 26/26 passed.pnpm exec vitest run --config config/vitest.config.ts src/main/runtime/rpc— 652/652 passed.pnpm run typecheck— passed across node, CLI, and web targets.AI Review Report
Reviewed the connection lifecycle, raw TCP admission, WebSocket upgrade cap, pre-auth timeout, heartbeat, over-capacity rejection, shutdown, and timer/error cleanup paths. The review also checked message-path and heartbeat costs, reconnect-flood behavior, and the Node/
ws/libuv behavior behind the proposed recovery claims.The review found and removed an unsafe accept-error handler: on Windows, a real libuv accept failure can stop the listener, so swallowing the forwarded error would mask a fatal listener state without restarting it. The accompanying test only emitted a synthetic event and did not exercise a stopped acceptor. The remaining tests now assert the exact TCP budget and the one-second forced-termination deadline.
Cross-platform behavior was checked for macOS/Linux libuv descriptor-exhaustion handling and Windows AcceptEx failure semantics. This PR does not touch shortcuts, labels, paths, shell behavior, SSH routing, or Git behavior.
Security Audit
The change reduces denial-of-service exposure by bounding unauthenticated raw/pre-upgrade connections. The existing 1 MiB WebSocket payload cap, ten-second pre-auth timeout, per-device authentication, and E2EE wiring are unchanged. No new input parsing, command execution, path handling, IPC surface, secrets, or dependencies are introduced.
Notes
The production evidence conclusively showed a stalled accept path (
SYN_RCVD, zeroESTABLISHED, including localhost), but did not capture the exact kernel/libuv trigger or proveEMFILE. This PR is a bounded-resource mitigation for the concrete connection-accumulation failure mode; it intentionally does not claim a generic listener restart or make all process-wide descriptor exhaustion impossible.Client endpoint advancement, relay egress fallback, and richer active-path diagnostics remain separate follow-ups.