Skip to content

Commit 35753a4

Browse files
maria-rcksZheruel
andcommitted
fix(client-runtime): keep healthy sessions on their current route
Co-authored-by: Tin Zeljar <[email protected]>
1 parent 87ea18c commit 35753a4

5 files changed

Lines changed: 129 additions & 144 deletions

File tree

‎docs/internals/connection-runtime.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ 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+
A healthy session stays on its current route: discovery does not prove that a
20+
better route can establish a socket. Necessary reconnects use the latest ranked
21+
routes, including those learned from the current session.
22+
1923
Foregrounding, an explicit retry, and an offline report probe the established
2024
session, and only a failed probe reconnects. Offline reports are often wrong, for
2125
example for a loopback server. A long mobile background suspension is the one

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

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ import type {
1414
} from "./model.ts";
1515
import { ConnectionTransientError } from "./model.ts";
1616
import * as ConnectionResolver from "./resolver.ts";
17-
import {
18-
connectionRoutes,
19-
routeEntry,
20-
routeHttpBaseUrl,
21-
sendsTokenInClear,
22-
usableRoutes,
23-
} from "./routes.ts";
17+
import { connectionRoutes, routeEntry, routeHttpBaseUrl, usableRoutes } from "./routes.ts";
2418
import * as RpcSession from "../rpc/session.ts";
2519
import { fetchRemoteEnvironmentDescriptor } from "../environment/descriptor.ts";
2620

@@ -42,8 +36,7 @@ export type RouteCheck = "answered" | "silent" | "unchecked";
4236

4337
const ROUTE_CHECK_TIMEOUT_MS = 2_500;
4438
// Preferred direct routes share one head start, so a relay connection does not
45-
// wait the full probe timeout for a LAN the device has already left. A route
46-
// that misses it connects over relay, then upgrades with a second reconnect.
39+
// wait the full probe timeout for a LAN the device has already left.
4740
const ROUTE_CHECK_HEAD_START_MS = 500;
4841

4942
export class ConnectionDriver extends Context.Service<
@@ -57,10 +50,6 @@ export class ConnectionDriver extends Context.Service<
5750
entry: ConnectionCatalogEntry,
5851
route: ConnectionRoute,
5952
) => Effect.Effect<RouteCheck>;
60-
readonly preflight: (
61-
entry: ConnectionCatalogEntry,
62-
route: ConnectionRoute,
63-
) => Effect.Effect<boolean>;
6453
}
6554
>()("@t3tools/client-runtime/connection/driver/ConnectionDriver") {}
6655

@@ -187,23 +176,7 @@ export const make = Effect.gen(function* () {
187176
);
188177
});
189178

190-
const preflight = (entry: ConnectionCatalogEntry, route: ConnectionRoute) =>
191-
(sendsTokenInClear(connectionRoutes(entry), route)
192-
? Effect.succeed("silent" as const)
193-
: checkRoute(entry, route)
194-
).pipe(
195-
Effect.flatMap((check) =>
196-
check === "answered"
197-
? resolver.prepare(routeEntry(entry, route)).pipe(
198-
Effect.as(true),
199-
Effect.orElseSucceed(() => false),
200-
)
201-
: Effect.succeed(false),
202-
),
203-
Effect.withSpan("ConnectionDriver.preflight"),
204-
);
205-
206-
return ConnectionDriver.of({ connect, checkRoute, preflight });
179+
return ConnectionDriver.of({ connect, checkRoute });
207180
});
208181

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,6 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
430430
),
431431
),
432432
checkRoute: (_entry, route) => checkRoute(route),
433-
preflight: (_entry, route) =>
434-
checkRoute(route).pipe(Effect.map((check) => check === "answered")),
435433
});
436434

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

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

Lines changed: 119 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { AuthStandardClientScopes, EnvironmentId, type ServerConfig } from "@t3t
22
import { describe, expect, it } from "@effect/vitest";
33
import * as Deferred from "effect/Deferred";
44
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
6+
import * as Scope from "effect/Scope";
57
import * as Layer from "effect/Layer";
68
import * as Option from "effect/Option";
79
import * as Random from "effect/Random";
@@ -20,6 +22,7 @@ import { fetchEnvironmentSessionState } from "../state/session.ts";
2022
import type { ConnectionCatalogEntry, ConnectionRoute } from "./catalog.ts";
2123
import * as Connectivity from "./connectivity.ts";
2224
import * as ConnectionDriver from "./driver.ts";
25+
import * as ConnectionResolver from "./resolver.ts";
2326
import { BearerConnectionProfile } from "./catalog.ts";
2427
import {
2528
BearerConnectionTarget,
@@ -231,8 +234,6 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
231234
ConnectionDriver.ConnectionDriver.of({
232235
connect,
233236
checkRoute: (_entry, route) => checkRoute(route),
234-
preflight: (_entry, route) =>
235-
checkRoute(route).pipe(Effect.map((check) => check === "answered")),
236237
}),
237238
),
238239
);
@@ -1003,6 +1004,111 @@ describe("EnvironmentSupervisor", () => {
10031004
});
10041005

10051006
describe("EnvironmentSupervisor routes", () => {
1007+
it.effect.each(["opening", "synchronizing"] as const)(
1008+
"keeps a healthy tailnet session when LAN passes preflight but fails %s",
1009+
(failureStage) =>
1010+
Effect.gen(function* () {
1011+
const tailnetTarget = new BearerConnectionTarget({
1012+
environmentId: TARGET.environmentId,
1013+
label: TARGET.label,
1014+
connectionId: "bearer:tailnet",
1015+
});
1016+
const entry = entryWithRoutes(LAN_THEN_RELAY_ENTRY, [
1017+
LAN_ROUTE,
1018+
{
1019+
target: tailnetTarget,
1020+
profile: Option.some(
1021+
new BearerConnectionProfile({
1022+
connectionId: tailnetTarget.connectionId,
1023+
environmentId: TARGET.environmentId,
1024+
label: TARGET.label,
1025+
httpBaseUrl: "http://100.64.0.1:3773/",
1026+
wsBaseUrl: "ws://100.64.0.1:3773/",
1027+
network: "tailscale",
1028+
}),
1029+
),
1030+
},
1031+
]);
1032+
const harness = yield* makeHarness();
1033+
const opened = yield* Ref.make<ReadonlyArray<string>>([]);
1034+
const released = yield* Ref.make<ReadonlyArray<string>>([]);
1035+
const driver = yield* ConnectionDriver.make.pipe(
1036+
Effect.provideService(ConnectionResolver.ConnectionResolver, {
1037+
prepare: (route) => Effect.succeed(preparedFor(route.target)),
1038+
prepareForUpdate: () => Effect.die("unused"),
1039+
}),
1040+
Effect.provideService(RpcSession.RpcSessionFactory, {
1041+
connect: (prepared) =>
1042+
Effect.gen(function* () {
1043+
const id = connectionRouteId(prepared.target);
1044+
yield* Ref.update(opened, (ids) => [...ids, id]);
1045+
const fails = id === LAN_TARGET.connectionId;
1046+
if (fails && failureStage === "opening") return yield* transient();
1047+
return yield* Effect.acquireRelease(
1048+
Effect.succeed({
1049+
client: TEST_RPC_CLIENT,
1050+
initialConfig: Effect.die("unused"),
1051+
subscribeServerConfig: () => Stream.die("unused"),
1052+
ready: fails ? Effect.fail(transient()) : Effect.void,
1053+
probe: Effect.void,
1054+
closed: Effect.never,
1055+
} satisfies RpcSession.RpcSession),
1056+
() => Ref.update(released, (ids) => [...ids, id]),
1057+
);
1058+
}),
1059+
}),
1060+
Effect.provide(
1061+
RpcHttp.layerRemoteHttpClient((() =>
1062+
Promise.resolve(
1063+
Response.json({
1064+
environmentId: TARGET.environmentId,
1065+
label: TARGET.label,
1066+
platform: { os: "darwin", arch: "arm64" },
1067+
serverVersion: "0.0.0-test",
1068+
capabilities: {},
1069+
}),
1070+
)) satisfies typeof fetch),
1071+
),
1072+
);
1073+
const scope = yield* Scope.fork(yield* Scope.Scope);
1074+
const supervisor = yield* EnvironmentSupervisor.make(entry, {
1075+
initiallyDesired: true,
1076+
}).pipe(
1077+
Effect.provideService(ConnectionDriver.ConnectionDriver, driver),
1078+
Effect.provide(harness.dependencies),
1079+
Scope.provide(scope),
1080+
);
1081+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1082+
const original = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session));
1083+
const baseline = yield* Ref.get(opened);
1084+
const baselineReleased = yield* Ref.get(released);
1085+
expect(baseline).toEqual([LAN_TARGET.connectionId, tailnetTarget.connectionId]);
1086+
expect(yield* driver.checkRoute(entry, LAN_ROUTE)).toBe("answered");
1087+
1088+
yield* harness.wake("network-changed");
1089+
yield* harness.setNetworkStatus("unknown");
1090+
yield* harness.setNetworkStatus("online");
1091+
yield* supervisor.retryNow;
1092+
yield* TestClock.adjust("12 minutes");
1093+
expect(yield* Ref.get(opened)).toEqual(baseline);
1094+
expect(yield* Ref.get(released)).toEqual(baselineReleased);
1095+
expect(Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session))).toBe(original);
1096+
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
1097+
phase: "connected",
1098+
generation: 1,
1099+
});
1100+
1101+
yield* Scope.close(scope, Exit.void);
1102+
expect(Option.isNone(yield* SubscriptionRef.get(supervisor.session))).toBe(true);
1103+
expect(
1104+
(yield* Ref.get(released)).filter((id) => id === tailnetTarget.connectionId),
1105+
).toHaveLength(1);
1106+
expect(
1107+
(yield* Ref.get(released)).filter((id) => id === LAN_TARGET.connectionId),
1108+
).toHaveLength(failureStage === "opening" ? 0 : 1);
1109+
}).pipe(Effect.provide(TestClock.layer())),
1110+
);
1111+
10061112
it.effect("connects over relay before a slow LAN reachability check times out", () =>
10071113
Effect.gen(function* () {
10081114
const checkingLan = yield* Deferred.make<void>();
@@ -1273,7 +1379,7 @@ describe("EnvironmentSupervisor routes", () => {
12731379
}),
12741380
);
12751381

1276-
it.effect("moves back to the LAN route when the network changes and it answers again", () =>
1382+
it.effect("uses a newly reachable LAN route on the next necessary connection", () =>
12771383
Effect.gen(function* () {
12781384
const lanReachable = yield* Ref.make(false);
12791385
const harness = yield* makeHarness({
@@ -1300,6 +1406,7 @@ describe("EnvironmentSupervisor routes", () => {
13001406
yield* Ref.set(lanReachable, true);
13011407
yield* harness.setNetworkStatus("unknown");
13021408
yield* harness.setNetworkStatus("online");
1409+
yield* harness.wake("application-active-reconnect");
13031410

13041411
yield* awaitState(
13051412
supervisor.state,
@@ -1354,7 +1461,7 @@ describe("EnvironmentSupervisor routes", () => {
13541461
}),
13551462
);
13561463

1357-
it.effect("learns the LAN address over T3 Connect and moves to it", () =>
1464+
it.effect("uses learned LAN routes on the next necessary connection", () =>
13581465
Effect.gen(function* () {
13591466
const relayEntry: ConnectionCatalogEntry = {
13601467
target: RELAY_TARGET,
@@ -1378,6 +1485,7 @@ describe("EnvironmentSupervisor routes", () => {
13781485
),
13791486
});
13801487
const current = yield* Ref.make(relayEntry);
1488+
const routesLearned = yield* Deferred.make<void>();
13811489
const supervisor = yield* EnvironmentSupervisor.make(relayEntry, {
13821490
initiallyDesired: true,
13831491
learnRoutes: ({ activeRoute, reported }) =>
@@ -1396,10 +1504,16 @@ describe("EnvironmentSupervisor routes", () => {
13961504
...all,
13971505
...routes.filter(isLearned).map((route) => connectionRouteId(route.target)),
13981506
]);
1507+
yield* Deferred.succeed(routesLearned, undefined);
13991508
return Option.some(next);
14001509
}),
14011510
}).pipe(Effect.provide(harness.dependencies));
14021511

1512+
yield* Deferred.await(routesLearned);
1513+
yield* TestClock.adjust("12 minutes");
1514+
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
1515+
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
1516+
yield* harness.wake("application-active-reconnect");
14031517
yield* awaitState(
14041518
supervisor.state,
14051519
(state) => state.phase === "connected" && state.generation === 2,
@@ -1410,6 +1524,6 @@ describe("EnvironmentSupervisor routes", () => {
14101524
expect(yield* Ref.get(learned)).toEqual([
14111525
`learned:${TARGET.environmentId}:http://192.168.1.10:3773`,
14121526
]);
1413-
}),
1527+
}).pipe(Effect.provide(TestClock.layer())),
14141528
);
14151529
});

0 commit comments

Comments
 (0)