Skip to content

fix(relay): stop self-closing the control socket on unknown messages - #18400

Merged
Jinwoo-H merged 2 commits into
mainfrom
sep03-relay-control-unknown-message
Sep 3, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
sep03-relay-control-unknown-message

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​45 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​45
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​12 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​11

Problem

Observed 2026-09-03 on a Samsung phone + macOS desktop (v1.4.197-adhoc): the desktop closed its own relay control WebSocket with code 4401 "unknown control message" after ~29 min active, then the phone could not reach the desktop for minutes — every dial ended in replacement session authentication timed out or relay_outer_4404 (HOST_OFFLINE), even after the director re-assigned the host 5s later.

control closed host=666077865f2e gen=1 state=active ageMs=1756183 code=4401 reason="unknown control message"

This is not branch-specific: relay telemetry shows ~15 of these 4401 "unknown control message" closes per day across app versions 1.4.175..1.4.197.

Root cause

RelayControlClient.handleMessage() (src/main/runtime/relay/relay-control-client.ts) routed every control frame that was not ping / conn-open / drain / a tracked request reply into failProtocol('unknown control message'), which closes the socket (4401) and tears down the origin.

Three well-formed frames reach that branch during a healthy active control:

  1. A reply that arrives after the desktop's 10s request deadline already deleted the pending entry. Relay control ops (confirmResume, installCredential, …) run DB transactions that can exceed 10s under load, so RelayControlRequests.resolveMessage() finds no waiter and returns false.
  2. A control-error with no/unknown reqId — including the relay's own sendControlError(session, undefined, 'unknown_control_message') reply to a host command it could not route (orca-cloud host-session-registry.ts).
  3. A newer relay opcode this build predates (forward-compat).

Why the phone stayed down for minutes

The 4401 self-close is far more costly than the frame that triggers it. On the relay the session drops to orphaned and, for the 30s orphan-grace window, acceptClient rejects the phone with HOST_OFFLINE (4404). The desktop's recovery (onClose → handleDrain(recovery: 'resolve-director')) then has to re-register through the director's 503 reconnect throttle, so a single stray frame becomes minutes of mobile downtime. Not self-closing removes the orphan window and the re-registration entirely.

Fix

A well-formed control frame the desktop does not recognize is now dropped and logged, not treated as fatal — matching how every other Orca decoder already handles an unknown frame. The relay control channel has no opcode-negotiation step, so the frame is either a newer relay message this build predates or a reply whose request already timed out. The justification is the cost asymmetry: self-closing orphaned the relay session and caused minutes of HOST_OFFLINE, whereas ignoring the frame costs nothing. Malformed JSON, binary frames, and messages before activation still close as genuine protocol violations.

(Note: an earlier revision cited remote-wire-compatibility.md Rule 2 as the mandate here. Rule 2 actually governs the sender of a new terminal-stream opcode and frames the receiver-side silent drop as a hazard, so that citation was dropped — thanks @pullfrog.)

Tests

  • ignores an unrecognized control message without closing the active control
  • ignores a reply whose request already timed out instead of self-closing
  • still tears down a malformed (non-JSON) control frame (guard preserved)

Verification

  • pnpm test relay-control-client.test.ts — 11/11
  • pnpm test relay-session-broker.test.ts — 8/8 (recovery path)
  • pnpm tc:node clean
  • pnpm run check:code-quality:changed — 0 findings

Compatibility

Desktop-only decoder change; nothing on the wire changes. Old relays (which already send these frames) and SSH/remote hosts are unaffected — the desktop simply stops overreacting to them.

The desktop control client tore its own relay control WebSocket down with
code 4401 "unknown control message" for any well-formed control frame it
did not recognize. handleMessage() funneled everything that was not
ping / conn-open / drain / a tracked request reply into
failProtocol('unknown control message'), which closes the socket and
orphans the origin.

Three real frames hit that branch:

- A relay reply that arrives after the desktop's 10s request deadline
  already deleted the pending entry. Relay control operations run DB
  transactions that can exceed 10s under load, so resolveMessage() finds
  no waiter and returns false.
- A control-error carrying no reqId (or an unknown one), including the
  relay's own 'unknown_control_message' reply to a host command it could
  not route.
- A newer relay's opcode that this build predates.

Fleet telemetry shows ~15 of these closes per day across app versions
1.4.175..1.4.197, so it is version-agnostic. The self-close was also far
more costly than the message that caused it: the relay session dropped to
'orphaned' and answered the phone with HOST_OFFLINE (4404) for the orphan
grace window, then the desktop had to re-register through the director's
503 reconnect throttle, stretching a single stray frame into minutes of
mobile downtime.

Per docs/reference/remote-wire-compatibility.md Rule 2, an unknown but
well-formed control frame must be dropped, not treated as fatal. Log and
ignore it; malformed JSON, binary frames, and messages before activation
still close as protocol violations.

Adds unit tests for the unknown-opcode drop, the timed-out-reply drop,
and the preserved malformed-frame teardown.
@coderabbitai

coderabbitai Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 7e224864-c96d-4b5b-aba5-ddb98467e932

📥 Commits

Reviewing files that changed from the base of the PR and between cdc2462 and 8739959.

📒 Files selected for processing (1)
  • src/main/runtime/relay/relay-control-client.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/runtime/relay/relay-control-client.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The relay control client now logs and ignores unknown well-formed control messages. Unknown opcodes and replies for expired requests leave the connection active. Malformed non-JSON frames still close the control and report BAD_OUTER_CREDENTIAL. Scripted-socket tests cover these behaviors.

Merge Risk: ⚪ Minimal · up to 87399

Well-formed unknown control messages and late replies now preserve the relay connection, while malformed frames retain teardown behavior. No current merge-blocking risk is identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, root cause, fix, tests, verification, and compatibility impact. However, it omits or leaves incomplete several template requirements, including ELI5, What Changed… Add the required template sections and complete them. Provide a valid linked issue after “Fixes #”. Add “Visual Proof” with N/A and a reason if no visual or interaction change applies. Complete the Testing checkboxes, AI Disclosure, Review,…
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
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 and concisely describes the primary change: preventing the relay control client from closing the control socket when it receives unknown messages.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

Full details: Description check

Explanation

The description explains the problem, root cause, fix, tests, verification, and compatibility impact. However, it omits or leaves incomplete several template requirements, including ELI5, What Changed, Why, a Linked Issue with an issue number, Visual Proof or an explicit N/A statement, AI Disclosure, Review, Agent skill upstream boundary, Notes, and the Checklist.

Resolution

Add the required template sections and complete them. Provide a valid linked issue after “Fixes #”. Add “Visual Proof” with N/A and a reason if no visual or interaction change applies. Complete the Testing checkboxes, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist.

  • Fix all pre-merge checks with AI
✨ 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.

@pullfrog pullfrog 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.

ℹ️ No critical issues — one doc-comment accuracy nit worth correcting, otherwise this is a clean, well-tested fix.

Reviewed changes

  • RelayControlClient.handleMessage — an unrecognized-but-well-formed control frame now logs and is dropped instead of failProtocol closing the socket with 4401 BAD_OUTER_CREDENTIAL. Malformed JSON, binary frames, and pre-activation frames still close as genuine protocol violations.
  • Three new tests: unknown-opcode drop, timed-out-reply drop, and the preserved malformed-JSON teardown guard.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

…tion

Rule 2 of remote-wire-compatibility governs the SENDER of a new terminal-
stream opcode and treats the receiver's silent drop as a hazard, not a
mandate. Reframe the comment around the actual justification: the decoder
convention of dropping unknown frames, the control channel's lack of an
opcode negotiation step, and the incident cost asymmetry.

@pullfrog pullfrog 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.

ℹ️ No new behavioral issues — the Rule 2 citation was cleaned up in the code comment and PR body, but one stale copy remains in a test comment (flagged inline).

Reviewed changes

  • Reworded the drop rationale in RelayControlClient.handleMessage — removed the remote-wire-compatibility.md Rule 2 citation (which misread Rule 2 as mandating "drop unknown, never fatal") and replaced it with the decoder-convention + no-negotiation-step + cost-asymmetry reasoning.
  • Updated the PR body to the same effect, noting the dropped citation.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

Comment on lines +561 to +563
// A newer relay opcode the desktop schema does not know. Rule 2 of
// remote-wire-compatibility: an unknown-but-well-formed frame is dropped,
// never fatal to a live control.

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.

This commit drops the Rule 2 misattribution from the code comment and PR body, but this test comment still cites it — and remote-wire-compatibility.md Rule 2 frames the receiver-side silent drop as a hazard to negotiate around, not as authority for "drop unknown, never fatal". Please update this copy so the test rationale matches the reworded code comment.

Suggested change
// A newer relay opcode the desktop schema does not know. Rule 2 of
// remote-wire-compatibility: an unknown-but-well-formed frame is dropped,
// never fatal to a live control.
// A newer relay opcode the desktop schema does not know. Dropped, never
// fatal to a live control — self-closing is costlier than ignoring (see the
// drop-branch rationale in relay-control-client.ts).

@Jinwoo-H
Jinwoo-H merged commit a35451f into main Sep 3, 2026
28 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#18400)

* fix(relay): stop self-closing the control socket on unknown messages

The desktop control client tore its own relay control WebSocket down with
code 4401 "unknown control message" for any well-formed control frame it
did not recognize. handleMessage() funneled everything that was not
ping / conn-open / drain / a tracked request reply into
failProtocol('unknown control message'), which closes the socket and
orphans the origin.

Three real frames hit that branch:

- A relay reply that arrives after the desktop's 10s request deadline
  already deleted the pending entry. Relay control operations run DB
  transactions that can exceed 10s under load, so resolveMessage() finds
  no waiter and returns false.
- A control-error carrying no reqId (or an unknown one), including the
  relay's own 'unknown_control_message' reply to a host command it could
  not route.
- A newer relay's opcode that this build predates.

Fleet telemetry shows ~15 of these closes per day across app versions
1.4.175..1.4.197, so it is version-agnostic. The self-close was also far
more costly than the message that caused it: the relay session dropped to
'orphaned' and answered the phone with HOST_OFFLINE (4404) for the orphan
grace window, then the desktop had to re-register through the director's
503 reconnect throttle, stretching a single stray frame into minutes of
mobile downtime.

Per docs/reference/remote-wire-compatibility.md Rule 2, an unknown but
well-formed control frame must be dropped, not treated as fatal. Log and
ignore it; malformed JSON, binary frames, and messages before activation
still close as protocol violations.

Adds unit tests for the unknown-opcode drop, the timed-out-reply drop,
and the preserved malformed-frame teardown.

* docs(relay): correct the ignore rationale, drop the Rule 2 misattribution

Rule 2 of remote-wire-compatibility governs the SENDER of a new terminal-
stream opcode and treats the receiver's silent drop as a hazard, not a
mandate. Reframe the comment around the actual justification: the decoder
convention of dropping unknown frames, the control channel's lack of an
opcode negotiation step, and the incident cost asymmetry.
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