Skip to content

Commit 9c70cb1

Browse files
t3dotggclaude
andcommitted
fix(client-runtime): a long resume reconnects while the network reports offline
A long resume replaced the session, but the supervisor does not reconnect while the network reports offline. After an offline report that a probe proved wrong, the client stayed disconnected until an online report. The replacement attempt now skips both offline checks once. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d79d4cc commit 9c70cb1

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎docs/internals/connection-runtime.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ session, and only a failed probe reconnects. Offline reports are often wrong, fo
2121
example for a loopback server. A long mobile background suspension is the one
2222
exception: it replaces the session at once, because the OS can kill a socket
2323
without reporting closure, and a probe would hold a dead socket in "Resuming"
24-
until it times out. Foregrounding also wakes a pending retry immediately and
24+
until it times out. That fresh attempt runs even while the network reports
25+
offline. Foregrounding also wakes a pending retry immediately and
2526
leaves an ordinary in-flight attempt alone.
2627

2728
The [registry](../../packages/client-runtime/src/connection/registry.ts) scopes

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,42 @@ describe("EnvironmentSupervisor", () => {
696696
}),
697697
);
698698

699+
it.effect("replaces the session on a long resume while the network reports offline", () =>
700+
Effect.gen(function* () {
701+
const probeCount = yield* Ref.make(0);
702+
const harness = yield* makeHarness({
703+
probe: () => Ref.update(probeCount, (count) => count + 1),
704+
});
705+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
706+
initiallyDesired: true,
707+
}).pipe(Effect.provide(harness.dependencies));
708+
709+
yield* awaitState(
710+
supervisor.state,
711+
(state) => state.phase === "connected" && state.generation === 1,
712+
);
713+
// A wrong offline report: the probe answers, so the session stays.
714+
yield* harness.setNetworkStatus("offline");
715+
for (let attempt = 0; attempt < 100; attempt += 1) {
716+
if ((yield* Ref.get(probeCount)) > 0) break;
717+
yield* Effect.yieldNow;
718+
}
719+
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
720+
721+
// The replacement connects although the network still reports offline.
722+
yield* harness.wake("application-active-reconnect");
723+
const replaced = yield* awaitState(
724+
supervisor.state,
725+
(state) => state.phase === "connected" && state.generation === 2,
726+
);
727+
728+
expect(replaced.attempt).toBe(1);
729+
expect(yield* Ref.get(probeCount)).toBe(1);
730+
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
731+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
732+
}),
733+
);
734+
699735
it.effect(
700736
"releases a session that stops answering while offline and reconnects when online",
701737
() =>

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
521521
generation: number,
522522
lastFailure: ConnectionAttemptError | null,
523523
pendingRetry: Option.Option<PendingRetryTrace>,
524+
ignoreOffline: boolean,
524525
) {
525526
yield* SubscriptionRef.set(prepared, Option.none());
526527
const establishment = yield* Effect.raceAllFirst([
@@ -586,7 +587,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
586587

587588
const active = establishment.exit.value;
588589
const currentIntent = yield* Ref.get(intent);
589-
if (!currentIntent.desired || currentIntent.network === "offline") {
590+
if (!currentIntent.desired || (currentIntent.network === "offline" && !ignoreOffline)) {
590591
return {
591592
_tag: "Interrupted",
592593
established: false,
@@ -671,6 +672,10 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
671672
failureCount = 0;
672673
pendingRetry = Option.none();
673674
};
675+
// Set after a long resume ends an attempt or a session. The fresh attempt
676+
// runs even while the network reports offline: the report is often wrong,
677+
// and the replaced session must not leave the client offline.
678+
let replacing = false;
674679

675680
for (;;) {
676681
if (yield* Ref.getAndSet(resetRetryState, false)) {
@@ -687,7 +692,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
687692
yield* waitForSignal;
688693
continue;
689694
}
690-
if (currentIntent.network === "offline") {
695+
if (currentIntent.network === "offline" && !replacing) {
691696
yield* clearLease;
692697
yield* setState(offlineState(currentIntent, generation, failureCount + 1, latestFailure));
693698
const applicationActivated = yield* waitForSignal;
@@ -700,8 +705,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
700705
const attempt = failureCount + 1;
701706
const nextGeneration = generation + 1;
702707
const outcome: AttemptOutcome = yield* Effect.scoped(
703-
runAttempt(attempt, nextGeneration, latestFailure, pendingRetry),
708+
runAttempt(attempt, nextGeneration, latestFailure, pendingRetry, replacing),
704709
);
710+
replacing = false;
705711
// Consumed on every iteration so a stale marker can never leak into a
706712
// later, unrelated failure.
707713
const failedProbe = yield* Ref.getAndSet(probeUnanswered, false);
@@ -715,6 +721,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
715721
if (outcome._tag === "Interrupted") {
716722
if (outcome.resetRetry) {
717723
resetRetryLadder();
724+
replacing = true;
718725
}
719726
continue;
720727
}

0 commit comments

Comments
 (0)