Skip to content

Commit 9122df2

Browse files
fix(client-runtime): primary setup survives an offline report and restarts when the network returns
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 092586d commit 9122df2

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,32 @@ describe("EnvironmentSupervisor", () => {
368368
}),
369369
);
370370

371+
it.effect(
372+
"keeps a primary attempt through an offline report and restarts it when the network returns",
373+
() =>
374+
Effect.gen(function* () {
375+
const started = yield* Deferred.make<void>();
376+
const harness = yield* makeHarness({
377+
prepare: (attempt) =>
378+
attempt === 1
379+
? Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never))
380+
: Effect.succeed(PREPARED_CONNECTION),
381+
});
382+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
383+
initiallyDesired: true,
384+
}).pipe(Effect.provide(harness.dependencies));
385+
386+
yield* Deferred.await(started);
387+
yield* harness.setNetworkStatus("offline");
388+
yield* TestClock.adjust("1 second");
389+
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
390+
391+
yield* harness.setNetworkStatus("online");
392+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
393+
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
394+
}).pipe(Effect.provide(TestClock.layer())),
395+
);
396+
371397
it.effect("keeps the latest failure visible throughout the next connection attempt", () =>
372398
Effect.gen(function* () {
373399
const harness = yield* makeHarness({

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,24 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
575575
});
576576

577577
const waitForEstablishmentInterrupt = Effect.fnUntraced(function* () {
578+
// A primary attempt keeps running through an offline report, so the
579+
// network returning restarts it in case it stalled meanwhile.
580+
let deviceOffline = (yield* connectivity.status) === "offline";
578581
for (;;) {
579582
const next = yield* Queue.take(signals);
580583
switch (next._tag) {
581584
case "DisconnectRequested":
582585
case "RetryRequested":
583586
return false;
584587
case "NetworkChanged":
585-
if (next.network === "offline") {
586-
return false;
588+
if (target._tag !== "PrimaryConnectionTarget") {
589+
if (next.network === "offline") {
590+
return false;
591+
}
592+
} else if (next.network === "offline") {
593+
deviceOffline = true;
594+
} else if (deviceOffline) {
595+
return true;
587596
}
588597
break;
589598
case "ConnectRequested":

0 commit comments

Comments
 (0)