Skip to content

Commit 0a649f9

Browse files
committed
packages/client-runtime/connection: open the replacement before closing the old session
The supervisor ended a session before it started the next one, on resume, on network changes and on route upgrades. On a remote route a resume waited out the 2.5 s silence window before any new connection began, and every switch showed "Syncing threads…" even when the host was reachable. Fixes #30833.
1 parent 8b44f42 commit 0a649f9

4 files changed

Lines changed: 867 additions & 130 deletions

File tree

‎docs/internals/connection-runtime.md‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,35 @@ client that can never connect retries all day. Offline states and authentication
1717
failures wait for a wakeup instead of spending attempts on unchanged conditions.
1818

1919
Foregrounding, a network change, an explicit retry, and an offline report probe
20-
the established session, and only a failed probe reconnects. Offline reports are
21-
often wrong, for example for a loopback server. The OS can kill a suspended
22-
socket without a close event, so a probe still unanswered after 500 ms asks the
23-
host for its public descriptor over a fresh request, again each second until it
24-
answers. If the host answers while the socket delivers no frame, the socket is
25-
dead and is replaced, even while the network reports offline: at once on
26-
loopback and LAN, and 2.5 seconds after the check starts elsewhere, where one
27-
lost packet can stall a live socket. Without that evidence the probe deadline
28-
decides: 3 seconds on loopback and LAN routes and after a long mobile
29-
suspension, when the data on screen is stale; 15 seconds elsewhere, because a
30-
tailnet over DERP or a VPN can be slower than that on a live socket. A long
31-
suspension's probe that times out also replaces the socket regardless of the
32-
network report. The web and desktop foreground probe skips the fresh request,
33-
because a busy host can answer it before a queued probe. Foregrounding also
34-
wakes a pending retry immediately. A long suspension restarts an in-flight
35-
attempt; a short one leaves it alone.
20+
the established session instead of tearing it down. Offline reports are often
21+
wrong, for example for a loopback server. The OS can kill a suspended socket
22+
without a close event, so a probe still unanswered after 500 ms asks the host
23+
for its public descriptor over a fresh request, again each second until it
24+
answers. If the host answers while the socket delivers no frame, a replacement
25+
session starts connecting. The socket itself is declared dead, even while the
26+
network reports offline, immediately on loopback and LAN, and 2.5 seconds after
27+
the check starts elsewhere, where one lost packet can stall a live socket.
28+
Without that evidence the probe deadline decides: 3 seconds on loopback and LAN
29+
routes and after a long mobile suspension, when the data on screen is stale; 15
30+
seconds elsewhere, because a tailnet over DERP or a VPN can be slower than that
31+
on a live socket. A long suspension's probe that times out also replaces the
32+
socket regardless of the network report. The web and desktop foreground probe
33+
skips the fresh request, because a busy host can answer it before a queued
34+
probe. Foregrounding also wakes a pending retry immediately. A long suspension
35+
restarts an in-flight attempt; a short one leaves it alone. A long suspension
36+
also restarts a pending replacement, which may have stalled with the app.
37+
38+
A replacement that connects first takes over the session directly, so
39+
subscriptions move without the client showing a reconnect, unless it landed on a
40+
worse route than the live socket; then it waits as the fallback. One still
41+
connecting when the socket is declared dead becomes the reconnect attempt; one
42+
that is not an upgrade closes when the live socket proves alive. A network
43+
change restarts or starts a replacement at once, except on loopback, because it
44+
can strand the socket's route, for example when leaving a LAN, and anything
45+
opened over the old path; that costs one extra connection per environment per
46+
change. A better route connects beside the live session the same way and takes
47+
over when it connects on that route; if it fails, the live session stays and the
48+
route waits five minutes.
3649

3750
The [registry](../../packages/client-runtime/src/connection/registry.ts) scopes
3851
connections by environment. An involuntary disconnect retains the registration

‎packages/client-runtime/src/connection/driver.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export type RoutePreflight = "ready" | "unprepared" | "silent" | "unchecked";
4747
const ROUTE_CHECK_TIMEOUT_MS = 2_500;
4848
// Preferred direct routes share one head start, so a relay connection does not
4949
// wait the full probe timeout for a LAN the device has already left. A route
50-
// that misses it connects over relay, then upgrades with a second reconnect.
50+
// that misses it connects over relay, then upgrades over a second connection.
5151
const ROUTE_CHECK_HEAD_START_MS = 500;
5252

5353
export class ConnectionDriver extends Context.Service<

0 commit comments

Comments
 (0)