diff --git a/packages/client-runtime/src/connection/supervisor.test.ts b/packages/client-runtime/src/connection/supervisor.test.ts index 0306a86ef49ae..43638ace42121 100644 --- a/packages/client-runtime/src/connection/supervisor.test.ts +++ b/packages/client-runtime/src/connection/supervisor.test.ts @@ -98,6 +98,25 @@ const LAN_THEN_RELAY_ENTRY: ConnectionCatalogEntry = { enabled: true, }; +const TAILNET_TARGET = new BearerConnectionTarget({ + environmentId: TARGET.environmentId, + label: TARGET.label, + connectionId: "bearer:tailnet", +}); +const TAILNET_ENTRY: ConnectionCatalogEntry = { + target: TAILNET_TARGET, + profile: Option.some( + new BearerConnectionProfile({ + connectionId: TAILNET_TARGET.connectionId, + environmentId: TARGET.environmentId, + label: TARGET.label, + httpBaseUrl: "http://workstation.tail1234.ts.net:3773/", + wsBaseUrl: "ws://workstation.tail1234.ts.net:3773/", + }), + ), + enabled: true, +}; + function preparedFor(target: ConnectionTarget): PreparedConnection { return { ...PREPARED_CONNECTION, target }; } @@ -755,6 +774,77 @@ describe("EnvironmentSupervisor", () => { }).pipe(Effect.provide(TestClock.layer())), ); + it.effect("quickly times out a stalled mobile foreground liveness probe on a LAN route", () => + Effect.gen(function* () { + const harness = yield* makeHarness({ + prepare: (_attempt, target) => Effect.succeed(preparedFor(target)), + probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void), + }); + const supervisor = yield* EnvironmentSupervisor.make( + { target: LAN_ROUTE.target, profile: LAN_ROUTE.profile, enabled: true }, + { initiallyDesired: true }, + ).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState(supervisor.state, (state) => state.phase === "connected"); + yield* harness.wake("application-active-probe"); + yield* TestClock.adjust("3 seconds"); + yield* awaitState( + supervisor.state, + (state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1, + ); + + expect(yield* Ref.get(harness.sessionCount)).toBe(2); + }).pipe(Effect.provide(TestClock.layer())), + ); + + it.effect("keeps a tailnet session whose foreground probe answers after a relay spike", () => + Effect.gen(function* () { + const harness = yield* makeHarness({ + prepare: (_attempt, target) => Effect.succeed(preparedFor(target)), + // A DERP-relayed round trip that spikes past the 3 second LAN budget. + probe: () => Effect.sleep("3500 millis"), + }); + const supervisor = yield* EnvironmentSupervisor.make(TAILNET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState(supervisor.state, (state) => state.phase === "connected"); + yield* harness.wake("application-active-probe"); + yield* TestClock.adjust("3500 millis"); + yield* harness.wake("network-changed"); + yield* TestClock.adjust("3500 millis"); + + const state = yield* SubscriptionRef.get(supervisor.state); + expect(state.phase).toBe("connected"); + expect(state.generation).toBe(1); + expect(yield* Ref.get(harness.sessionCount)).toBe(1); + }).pipe(Effect.provide(TestClock.layer())), + ); + + it.effect("replaces a tailnet session whose foreground probe stalls past the full budget", () => + Effect.gen(function* () { + const harness = yield* makeHarness({ + prepare: (_attempt, target) => Effect.succeed(preparedFor(target)), + probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void), + }); + const supervisor = yield* EnvironmentSupervisor.make(TAILNET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState(supervisor.state, (state) => state.phase === "connected"); + yield* harness.wake("application-active-probe"); + yield* TestClock.adjust("3 seconds"); + expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(1); + + yield* TestClock.adjust("12 seconds"); + yield* awaitState( + supervisor.state, + (state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1, + ); + expect(yield* Ref.get(harness.sessionCount)).toBe(2); + }).pipe(Effect.provide(TestClock.layer())), + ); + it.effect("honors an explicit disconnect while a foreground probe is stalled", () => Effect.gen(function* () { const probeStarted = yield* Deferred.make(); diff --git a/packages/client-runtime/src/connection/supervisor.ts b/packages/client-runtime/src/connection/supervisor.ts index d500fe6d8347c..6d9a2c40db6ff 100644 --- a/packages/client-runtime/src/connection/supervisor.ts +++ b/packages/client-runtime/src/connection/supervisor.ts @@ -35,6 +35,7 @@ import { connectionRouteKind, connectionRoutes, entryWithRoutes, + type ConnectionRouteKind, type ReportedEndpoint, } from "./routes.ts"; @@ -124,6 +125,16 @@ export interface EnvironmentSupervisorOptions { }) => Effect.Effect>; } +// A wakeup probe checks a socket the OS may have killed silently. Only loopback +// and LAN routes answer within the quick budget reliably; a DERP-relayed tailnet, +// VPN or public route spikes past 3 seconds, and treating that as a dead socket +// tears down a working session and forces a full resync. +function wakeupProbeTimeout(routeKind: ConnectionRouteKind | undefined): Duration.Input { + return routeKind === "loopback" || routeKind === "lan" + ? QUICK_CONNECTION_PROBE_TIMEOUT + : CONNECTION_PROBE_TIMEOUT; +} + function retryDelayMs(failureCount: number, random: number): number { const ceiling = Math.min(RETRY_MAX_DELAY_MS, RETRY_BASE_DELAY_MS * 2 ** (failureCount + 1)); return Math.round(ceiling / 2 + (ceiling / 2) * random); @@ -320,6 +331,17 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( ), ); + const leaseRouteKind = (lease: ConnectionDriver.EnvironmentConnectionLease) => + currentRoutes.pipe( + Effect.map((routes) => { + const route = routes.find( + (candidate) => + connectionRouteId(candidate.target) === connectionRouteId(lease.prepared.target), + ); + return route === undefined ? undefined : connectionRouteKind(route); + }), + ); + const checkBetterRoutes = Effect.fnUntraced(function* ( lease: ConnectionDriver.EnvironmentConnectionLease, ) { @@ -564,7 +586,10 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( return undefined; }); - const probeTimeoutFor = (next: SupervisorSignal): Duration.Input | undefined => { + const probeTimeoutFor = ( + next: SupervisorSignal, + routeKind: ConnectionRouteKind | undefined, + ): Duration.Input | undefined => { switch (next._tag) { case "RetryRequested": return QUICK_CONNECTION_PROBE_TIMEOUT; @@ -575,7 +600,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( return CONNECTION_PROBE_TIMEOUT; } return next.reason === "application-active-probe" || next.reason === "network-changed" - ? QUICK_CONNECTION_PROBE_TIMEOUT + ? wakeupProbeTimeout(routeKind) : undefined; case "ConnectRequested": case "DisconnectRequested": @@ -605,6 +630,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( Stream.runForEach(() => requestBetterRouteCheck(lease)), Effect.forkScoped, ); + const routeKind = yield* leaseRouteKind(lease); for (;;) { const next = yield* takeSignal; const end = yield* connectedLeaseEnd(next, lease); @@ -617,7 +643,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( ) { yield* requestBetterRouteCheck(lease); } - const probeTimeout = probeTimeoutFor(next); + const probeTimeout = probeTimeoutFor(next, routeKind); if (probeTimeout === undefined) { continue; } @@ -654,7 +680,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( yield* Fiber.interrupt(probe); return endDuringProbe === "reset"; } - const signalTimeout = probeTimeoutFor(probeEvent.signal); + const signalTimeout = probeTimeoutFor(probeEvent.signal, routeKind); if (signalTimeout !== undefined) { const signalDeadline = (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(signalTimeout);