Skip to content

fix(mobile): harden WebSocket accept path against socket overload - #9247

Merged
brennanb2025 merged 2 commits into
mainfrom
brennanb2025/fix-mobile-ws-accept-wedge
Jul 18, 2026
Merged

brennanb2025 merged 2 commits into
mainfrom
brennanb2025/fix-mobile-ws-accept-wedge

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The desktop mobile WebSocket listener could remain in LISTEN while new TCP handshakes stopped completing, disconnecting the persistent channel used for mobile RPC and notifications.

This change hardens the listener against connection accumulation:

  • Caps all TCP sockets at 256, above the 128 upgraded-WebSocket budget, so raw and pre-upgrade clients cannot consume descriptors without bound.
  • Gives upgraded sockets over the WebSocket cap a 1013 close, then force-terminates them after one second so reconnect floods cannot retain rejected sockets until the next 15-second heartbeat.
  • Logs heartbeat reaping and capacity pressure without adding work to normal RPC message handling.

Screenshots

No visual change.

Testing

  • pnpm lint — changed files pass; the repository-wide command reaches two unrelated existing switch-exhaustiveness errors in native-chat-session-option-labels.ts and skill-freshness-group.tsx.
  • pnpm typecheck
  • pnpm test — focused transport and full runtime RPC suites were run instead.
  • pnpm build — not needed for this isolated main-process transport change.
  • Added or updated high-quality tests that would catch regressions.

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.
  • Changed-file oxlint, diff check, and max-lines ratchet — passed.

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, zero ESTABLISHED, including localhost), but did not capture the exact kernel/libuv trigger or prove EMFILE. 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.

…-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.
@coderabbitai

coderabbitai Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The 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)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: hardening the WebSocket accept path against overload.
Description check ✅ Passed The description includes all required sections and covers testing, review, security, screenshots, and notes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 84b9b38a-5502-448e-bb9b-e0fc7acb68dd

📥 Commits

Reviewing files that changed from the base of the PR and between 613dcd8 and 0d2c41d.

📒 Files selected for processing (2)
  • src/main/runtime/rpc/ws-transport.test.ts
  • src/main/runtime/rpc/ws-transport.ts

@brennanb2025 brennanb2025 changed the title fix(mobile): make the :6768 WebSocket accept loop leak-proof and self-healing fix(mobile): harden WebSocket accept path against socket overload Jul 17, 2026
@brennanb2025
brennanb2025 merged commit 49e625d into main Jul 18, 2026
1 check passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant