Skip to content

Commit 0eb2ce6

Browse files
t3dotggomar7550broomvaclaude
committed
fix(client-runtime): resume keeps live sockets and replaces dead ones fast
A resume judged the socket by the clock: a short resume waited out a 3 s probe on a socket iOS had killed, a long one replaced even a healthy socket and resynced, and a slow remote route lost a live socket after 3 s. Every resume now probes the socket. A probe unanswered after 500 ms asks the host for its public descriptor over a fresh request, retrying each second. If the host answers while the socket delivers no frame, the socket is replaced: at once on loopback and LAN, after 2.5 s elsewhere. Remote routes give a live socket 15 s on a short resume or network change. Co-authored-by: Omar <[email protected]> Co-authored-by: Carlos D. Escobar-Valbuena <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2202787 commit 0eb2ce6

23 files changed

Lines changed: 566 additions & 64 deletions
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import type { Wakeups } from "@t3tools/client-runtime/connection";
22

3-
export const MOBILE_BACKGROUND_RECONNECT_AFTER_MS = 10_000;
3+
export const MOBILE_LONG_BACKGROUND_AFTER_MS = 10_000;
44

55
export type MobileApplicationActiveWakeup = Extract<
66
Wakeups.ConnectionWakeup,
7-
"application-active-probe" | "application-active-reconnect"
7+
"application-active-probe" | "application-active-long"
88
>;
99

1010
export function mobileApplicationActiveWakeup(
1111
backgroundedAtMs: number | null,
1212
activeAtMs: number,
1313
): MobileApplicationActiveWakeup {
1414
return backgroundedAtMs !== null &&
15-
activeAtMs - backgroundedAtMs >= MOBILE_BACKGROUND_RECONNECT_AFTER_MS
16-
? "application-active-reconnect"
15+
activeAtMs - backgroundedAtMs >= MOBILE_LONG_BACKGROUND_AFTER_MS
16+
? "application-active-long"
1717
: "application-active-probe";
1818
}

‎apps/mobile/src/connection/platform.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const networkPathChanges = Stream.callback<"network-changed">((queue) =>
118118
const layerWakeups = Wakeups.layer({
119119
changes: Stream.mergeAll(
120120
[
121-
Stream.callback<"application-active-probe" | "application-active-reconnect">((queue) =>
121+
Stream.callback<"application-active-probe" | "application-active-long">((queue) =>
122122
Effect.acquireRelease(
123123
Effect.sync(() => {
124124
let backgroundedAtMs = AppState.currentState === "background" ? Date.now() : null;

‎apps/web/src/cloud/linkEnvironment.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ function layerRegistry(options?: {
8585
subscribeServerConfig: (input) => client.subscribeServerConfig(input),
8686
ready: Effect.void,
8787
probe: Effect.void,
88+
inboundFrames: Effect.succeed(0),
8889
closed: Effect.never,
8990
};
9091
const target = new PrimaryConnectionTarget({

‎apps/web/test/fakeEnvironment.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ const session: EnvironmentConnectionLease["session"] = {
377377
subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig }]),
378378
ready: Effect.void,
379379
probe: Effect.void,
380+
inboundFrames: Effect.succeed(0),
380381
closed: Effect.never,
381382
};
382383

@@ -398,6 +399,7 @@ export const fakeDriver = {
398399
}),
399400
checkRoute: () => Effect.succeed<RouteCheck>("answered"),
400401
preflight: () => Effect.succeed(true),
402+
checkHost: () => Effect.succeed<RouteCheck>("unchecked"),
401403
};
402404
export type FakeDriver = typeof fakeDriver;
403405

‎docs/internals/connection-runtime.md‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ client of a restarted server reconnects in the same second; with a short cap, a
1616
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

19-
Foregrounding, an explicit retry, and an offline report probe the established
20-
session, and only a failed probe reconnects. Offline reports are often wrong, for
21-
example for a loopback server. A long mobile background suspension is the one
22-
exception: it replaces the session at once, because the OS can kill a socket
23-
without reporting closure, and a probe would hold a dead socket in "Resuming"
24-
until it times out. That fresh attempt runs even while the network reports
25-
offline. Foregrounding also wakes a pending retry immediately and
26-
leaves an ordinary in-flight attempt alone.
19+
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.
2736

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

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export class ConnectionDriver extends Context.Service<
6161
entry: ConnectionCatalogEntry,
6262
route: ConnectionRoute,
6363
) => Effect.Effect<boolean>;
64+
/** Asks the host behind a live session to answer a fresh request on the session's own origin. */
65+
readonly checkHost: (prepared: PreparedConnection) => Effect.Effect<RouteCheck>;
6466
}
6567
>()("@t3tools/client-runtime/connection/driver/ConnectionDriver") {}
6668

@@ -142,26 +144,37 @@ export const make = Effect.gen(function* () {
142144
const sessions = yield* RpcSession.RpcSessionFactory;
143145
const httpClient = yield* HttpClient.HttpClient;
144146

145-
const checkRoute = (entry: ConnectionCatalogEntry, route: ConnectionRoute) => {
146-
const httpBaseUrl = routeHttpBaseUrl(route);
147-
if (httpBaseUrl === null) return Effect.succeed<RouteCheck>("unchecked");
148-
// The descriptor is public, so this sends no credential to whatever
149-
// answers at a saved LAN address on a different network.
150-
return fetchRemoteEnvironmentDescriptor({
147+
// The descriptor is public, so this sends no credential to whatever
148+
// answers at a saved LAN address on a different network.
149+
const checkDescriptor = (httpBaseUrl: string, environmentId: string) =>
150+
fetchRemoteEnvironmentDescriptor({
151151
httpBaseUrl,
152152
timeoutMs: ROUTE_CHECK_TIMEOUT_MS,
153153
}).pipe(
154154
Effect.map((descriptor): RouteCheck =>
155-
descriptor.environmentId === entry.target.environmentId ? "answered" : "silent",
155+
descriptor.environmentId === environmentId ? "answered" : "silent",
156156
),
157157
Effect.orElseSucceed((): RouteCheck => "silent"),
158158
Effect.provideService(HttpClient.HttpClient, httpClient),
159+
);
160+
161+
const checkRoute = (entry: ConnectionCatalogEntry, route: ConnectionRoute) => {
162+
const httpBaseUrl = routeHttpBaseUrl(route);
163+
if (httpBaseUrl === null) return Effect.succeed<RouteCheck>("unchecked");
164+
return checkDescriptor(httpBaseUrl, entry.target.environmentId).pipe(
159165
Effect.withSpan("ConnectionDriver.checkRoute", {
160166
attributes: { "connection.target.kind": route.target._tag },
161167
}),
162168
);
163169
};
164170

171+
const checkHost = (prepared: PreparedConnection) =>
172+
checkDescriptor(prepared.httpBaseUrl, prepared.environmentId).pipe(
173+
Effect.withSpan("ConnectionDriver.checkHost", {
174+
attributes: { "connection.target.kind": prepared.target._tag },
175+
}),
176+
);
177+
165178
const connect = Effect.fn("ConnectionDriver.connect")(function* (
166179
entry: ConnectionCatalogEntry,
167180
reportProgress: (progress: ConnectionDriverProgress) => Effect.Effect<void>,
@@ -203,7 +216,7 @@ export const make = Effect.gen(function* () {
203216
Effect.withSpan("ConnectionDriver.preflight"),
204217
);
205218

206-
return ConnectionDriver.of({ connect, checkRoute, preflight });
219+
return ConnectionDriver.of({ connect, checkRoute, preflight, checkHost });
207220
});
208221

209222
export const layer = Layer.effect(ConnectionDriver, make);

‎packages/client-runtime/src/connection/registry.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
413413
Stream.die(new Error("Config is not used by registry tests.")),
414414
ready: Effect.void,
415415
probe: Effect.void,
416+
inboundFrames: Effect.succeed(0),
416417
closed: Deferred.await(closed),
417418
} satisfies RpcSession.RpcSession),
418419
() => Ref.update(releasedSessions, (count) => count + 1),
@@ -432,6 +433,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
432433
checkRoute: (_entry, route) => checkRoute(route),
433434
preflight: (_entry, route) =>
434435
checkRoute(route).pipe(Effect.map((check) => check === "answered")),
436+
checkHost: () => Effect.succeed<ConnectionDriver.RouteCheck>("unchecked"),
435437
});
436438

437439
const layerCache = Layer.succeed(Persistence.EnvironmentCacheStore, cacheStore);

0 commit comments

Comments
 (0)