Skip to content

fix(remote-connect): let Desktop own the shared device Relay socket, with CLI standby takeover - #3689

Merged
bobleer merged 3 commits into
openBitFun:mainfrom
bobleer:bob/relay-device-routing-priority
Nov 2, 2026
Merged

bobleer merged 3 commits into
openBitFun:mainfrom
bobleer:bob/relay-device-routing-priority

Conversation

@bobleer

@bobleer bobleer commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A Desktop and a CLI daemon signed in on the same machine no longer fight over the account device's Relay socket. Desktop takes priority. The CLI stands by while a Desktop serves the device, and reconnects by itself once the Desktop exits or is killed.

Type and Areas

Type: bug fix

Areas: Rust services (remote-connect), desktop/Tauri, CLI

Motivation / Impact

Every host on a machine shares device_identity.json, so they all authenticate as the same device_id. The Relay keeps only the newest machine-scoped socket per device (DeviceManager::activate_pending_inner force-closes the previous one). Both RelayClient::run loops reconnect within 0.5 to 5 seconds, so each host replaced the other's connection over and over. One daemon recorded about 110 disconnects in its first 5 minutes.

The fix is host-local arbitration in remote_connect::routing_lease. It uses OS advisory file locks (fs2, already a remote-connect dependency), one per Relay endpoint and priority tier, stored next to the identity file:

  • Primary (Desktop): connects whenever it holds the primary lock, and never yields to a secondary host. If it starts while another primary holds the lock, it waits up to 10s (covering a restarting process) and then reports a clear error.
  • Secondary (CLI, TUI, daemon): connects only while it holds the secondary lock and no primary holds its lock. Otherwise it stays in the new ConnectionState::Standby, and connect_authenticated still succeeds. A connected secondary yields as soon as the Relay replaces its socket: it checks the lease before every reconnect. It needs no polling while connected.
  • The OS releases a lock when its process exits, crashes or is killed, so there is no stale owner record. The standing-by host reconnects within about 1s.
  • Different Relay endpoints (official vs LAN) do not contend.
  • If the lock files cannot be used (an IO error), the client falls back to the previous unarbitrated behavior.

A CLI or app-server login that starts in standby now says that another host serves the device, instead of "Device routing connected." (AccountLoginResult::routing_standby).

Mostly a developer scenario. No user-facing change for single-instance users.

Verification

  • cargo test -p openbitfun-services-integrations --features remote-connect --lib remote_connect::r: 33 passed. New tests:
    • lease: primary preempts secondary; secondary resumes after primary exits; a standing-by secondary keeps its place in line; hosts of the same tier take turns; endpoints are independent
    • run loop: a secondary stands by without touching the Relay and connects once the primary lease drops; a primary connects while a secondary holds routing
  • cargo test -p openbitfun-cli --bin openbitfun account::tests: 4 passed, including the new check that a standby login does not claim connected routing
  • cargo check -p openbitfun-cli -p openbitfun-desktop -p openbitfun-app-server: no new warnings
  • pnpm run check:core-boundaries: passed

Remote scenarios: this only changes how local hosts arbitrate their own account device socket. The Relay protocol, controllers, Peer Device Mode and Dispatch wire contracts are unchanged. Exercised with unit and run-loop tests against a local TCP listener. Not yet exercised against a live Relay with a real Desktop and daemon running together.

Reviewer Notes

  • Compatibility: no persisted shape or protocol change. A host built before this change does not arbitrate, so an old daemon next to a new Desktop still replaces the Desktop's socket until both are upgraded.
  • Known limitation, not changed here: device_kind on the Relay row comes from the most recent login. After a Desktop exits and the daemon takes over, the device may still be labeled desktop until the CLI logs in again.
  • RelayClient::new() stays unarbitrated for tests and existing callers. Product hosts use RelayClient::new_device_host(priority).

Checklist

  • This PR is focused and does not include secrets, temporary prompts, generated scratch files, or unrelated artifacts.
  • Relevant verification is recorded above, or skipped checks are explained.
  • User-facing strings, docs, and locales are updated where applicable.

… local hosts

Every OpenBitFun host on a machine shares device_identity.json, and the Relay
keeps only the newest machine-scoped socket per device_id. A Desktop and a CLI
daemon that are both signed in therefore replaced each other's socket every
few seconds.

Add a host-local routing lease: an OS advisory file lock per Relay endpoint and
priority tier, stored next to the identity file. A primary host connects
whenever it holds its lock. A secondary host connects only while no primary
holds the primary lock, and otherwise stands by in the new Standby state. The
OS releases a lock when its process exits or is killed, so the standing-by
host takes over within about a second. Lock IO failures fall back to the
previous unarbitrated behavior. The Relay protocol is unchanged.
Desktop opens its account device socket as the primary host. CLI, TUI and the
daemon open theirs as secondary hosts: they stand by while a Desktop on the
same machine serves the device and reconnect once it exits.
A CLI login while a Desktop serves the shared device started routing in
standby but still reported "Device routing connected". Carry a
routing_standby fact on the login result and say that this host takes over
when the other one exits.
@bobleer
bobleer merged commit b311f4b into openBitFun:main Nov 2, 2026
11 of 12 checks passed
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