Skip to content

Commit 0b89cb1

Browse files
shivamhwpclaude
andcommitted
fix(connection): restart on network recovery after a route fails post-open
A route that opened its socket and then failed left the attempt marked open while the next route ran its reachability check or preparation, so a network recovery in that window was ignored. Route selection now reports preparing as soon as a route fails, which clears the open mark before the fallback starts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 862fcc3 commit 0b89cb1

3 files changed

Lines changed: 58 additions & 34 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export const connectOverRoutes = Effect.fn("ConnectionDriver.connectOverRoutes")
7878
connectRoute: (
7979
route: ConnectionRoute,
8080
) => Effect.Effect<EnvironmentConnectionLease, ConnectionAttemptError, R | Scope.Scope>,
81+
reportProgress: (progress: ConnectionDriverProgress) => Effect.Effect<void>,
8182
) {
83+
yield* reportProgress({ stage: "preparing" });
8284
const routes = usableRoutes(entry);
8385
const checks =
8486
routes.length === 1
@@ -99,6 +101,8 @@ export const connectOverRoutes = Effect.fn("ConnectionDriver.connectOverRoutes")
99101
if (result._tag === "Failure") {
100102
if (result.failure._tag === "ConnectionTransientError") transient ??= result.failure;
101103
else blocked ??= result.failure;
104+
// The failed route may have opened its socket; the next route starts over.
105+
yield* reportProgress({ stage: "preparing" });
102106
}
103107
return result;
104108
});
@@ -194,7 +198,6 @@ export const make = Effect.gen(function* () {
194198
"connection.target.kind": target._tag,
195199
"connection.route.count": connectionRoutes(entry).length,
196200
});
197-
yield* reportProgress({ stage: "preparing" });
198201
return yield* connectOverRoutes(
199202
entry,
200203
(route) => checkRoute(entry, route),
@@ -222,6 +225,7 @@ export const make = Effect.gen(function* () {
222225
}
223226
return { prepared, session } satisfies EnvironmentConnectionLease;
224227
}),
228+
reportProgress,
225229
);
226230
});
227231

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,11 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
438438
});
439439
const driver = ConnectionDriver.ConnectionDriver.of({
440440
connect: (entry, reportProgress) =>
441-
reportProgress({ stage: "preparing" }).pipe(
442-
Effect.andThen(
443-
ConnectionDriver.connectOverRoutes(entry, checkRoute, (route) =>
444-
connectRoute(entry, route),
445-
),
446-
),
441+
ConnectionDriver.connectOverRoutes(
442+
entry,
443+
checkRoute,
444+
(route) => connectRoute(entry, route),
445+
reportProgress,
447446
),
448447
preflight: (_entry, route) =>
449448
checkRoute(route).pipe(

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

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,11 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
235235
entry: ConnectionCatalogEntry,
236236
reportProgress: (progress: ConnectionDriver.ConnectionDriverProgress) => Effect.Effect<void>,
237237
) {
238-
yield* reportProgress({ stage: "preparing" });
239-
return yield* ConnectionDriver.connectOverRoutes(entry, checkRoute, (route) =>
240-
connectRoute(route.target, reportProgress),
238+
return yield* ConnectionDriver.connectOverRoutes(
239+
entry,
240+
checkRoute,
241+
(route) => connectRoute(route.target, reportProgress),
242+
reportProgress,
241243
);
242244
});
243245

@@ -1818,32 +1820,51 @@ describe("EnvironmentSupervisor routes", () => {
18181820
}),
18191821
);
18201822

1821-
it.effect("restarts on a network recovery while a fallback route has not opened", () =>
1822-
Effect.gen(function* () {
1823-
const fallbackOpening = yield* Deferred.make<void>();
1824-
const harness = yield* makeHarness({
1825-
networkStatus: "unknown",
1826-
checkRoute: () => Effect.succeed("answered"),
1827-
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1828-
opened: (attempt) =>
1829-
attempt === 2
1830-
? Deferred.succeed(fallbackOpening, undefined).pipe(Effect.andThen(Effect.never))
1831-
: Effect.void,
1832-
ready: (attempt) => (attempt === 1 ? Effect.fail(transient()) : Effect.void),
1833-
});
1834-
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
1835-
initiallyDesired: true,
1836-
}).pipe(Effect.provide(harness.dependencies));
1823+
// The LAN route opens and then fails its snapshot, so the attempt moves on to relay.
1824+
it.effect.each([
1825+
{ stall: "reachability check", sessions: 2 },
1826+
{ stall: "preparation", sessions: 2 },
1827+
{ stall: "opening", sessions: 3 },
1828+
] as const)(
1829+
"restarts on a network recovery while a fallback route stalls in $stall",
1830+
({ stall, sessions }) =>
1831+
Effect.gen(function* () {
1832+
const stalled = yield* Deferred.make<void>();
1833+
const stallHere = Deferred.succeed(stalled, undefined).pipe(Effect.andThen(Effect.never));
1834+
const harness = yield* makeHarness({
1835+
networkStatus: "unknown",
1836+
checkRoute: (route) =>
1837+
stall === "reachability check" && route.target._tag === "RelayConnectionTarget"
1838+
? Effect.never
1839+
: Effect.succeed("answered"),
1840+
prepare: (_attempt, target) =>
1841+
stall === "preparation" && target._tag === "RelayConnectionTarget"
1842+
? stallHere
1843+
: Effect.succeed(preparedFor(target)),
1844+
opened: (attempt) => (stall === "opening" && attempt === 2 ? stallHere : Effect.void),
1845+
ready: (attempt) =>
1846+
attempt !== 1
1847+
? Effect.void
1848+
: stall === "reachability check"
1849+
? Deferred.succeed(stalled, undefined).pipe(
1850+
Effect.andThen(Effect.fail(transient())),
1851+
)
1852+
: Effect.fail(transient()),
1853+
});
1854+
const supervisor = yield* EnvironmentSupervisor.make(LAN_THEN_RELAY_ENTRY, {
1855+
initiallyDesired: true,
1856+
}).pipe(Effect.provide(harness.dependencies));
18371857

1838-
yield* Deferred.await(fallbackOpening);
1839-
yield* harness.setNetworkStatus("online");
1840-
yield* TestClock.adjust("0 millis");
1858+
yield* Deferred.await(stalled);
1859+
yield* TestClock.adjust("0 millis");
1860+
yield* harness.setNetworkStatus("online");
1861+
yield* TestClock.adjust("0 millis");
18411862

1842-
expect(yield* Ref.get(harness.sessionCount)).toBe(3);
1843-
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1844-
const prepared = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared));
1845-
expect(prepared.target._tag).toBe("BearerConnectionTarget");
1846-
}).pipe(Effect.provide(TestClock.layer())),
1863+
expect(yield* Ref.get(harness.sessionCount)).toBe(sessions);
1864+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1865+
const prepared = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.prepared));
1866+
expect(prepared.target._tag).toBe("BearerConnectionTarget");
1867+
}).pipe(Effect.provide(TestClock.layer())),
18471868
);
18481869

18491870
it.effect("tries every route when none answers the reachability check", () =>

0 commit comments

Comments
 (0)