diff --git a/packages/client-runtime/src/connection/supervisor.test.ts b/packages/client-runtime/src/connection/supervisor.test.ts index 46b9757d0cd83..19ea686c6ee54 100644 --- a/packages/client-runtime/src/connection/supervisor.test.ts +++ b/packages/client-runtime/src/connection/supervisor.test.ts @@ -432,6 +432,33 @@ describe("EnvironmentSupervisor", () => { }), ); + it.effect("still tries while the device keeps reporting offline", () => + Effect.gen(function* () { + const harness = yield* makeHarness({ + networkStatus: "offline", + prepare: (attempt) => + attempt === 1 ? Effect.fail(transient()) : Effect.succeed(PREPARED_CONNECTION), + }); + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState(supervisor.state, (state) => state.phase === "offline"); + expect(yield* Ref.get(harness.prepareCount)).toBe(0); + // A recheck that fails while still reported offline stays offline, not backoff. + yield* TestClock.adjust("30 seconds"); + yield* awaitState( + supervisor.state, + (state) => state.phase === "offline" && state.attempt === 1 && state.lastFailure === null, + ); + expect(yield* Ref.get(harness.prepareCount)).toBe(1); + // The report never changes, but the host becomes reachable. + yield* TestClock.adjust("30 seconds"); + yield* awaitState(supervisor.state, (state) => state.phase === "connected"); + expect(yield* Ref.get(harness.prepareCount)).toBe(2); + }), + ); + it.effect("connects to the primary environment while the device reports offline", () => Effect.gen(function* () { const harness = yield* makeHarness({ networkStatus: "offline" }); diff --git a/packages/client-runtime/src/connection/supervisor.ts b/packages/client-runtime/src/connection/supervisor.ts index 58411d795b7fc..2db47f8c7a5f2 100644 --- a/packages/client-runtime/src/connection/supervisor.ts +++ b/packages/client-runtime/src/connection/supervisor.ts @@ -45,6 +45,7 @@ import { const RETRY_BASE_DELAY_MS = 1_000; const RETRY_MAX_DELAY_MS = 300_000; +const OFFLINE_RECHECK_MS = 30_000; const SESSION_LOSS_RETRY_DELAY_MS = 250; // Someone is approving this device on the host; check back soon rather than climbing the ladder. const APPROVAL_PENDING_RETRY_DELAY_MS = 4_000; @@ -1195,6 +1196,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( pendingRetry = Option.none(); }; let replacing = false; + let offlineRecheckDue = false; for (;;) { const retryRequested = yield* Ref.getAndSet(resetRetryState, false); @@ -1215,15 +1217,18 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( continue; } // The platform's offline report can stay wrong after the network is back, - // so an explicit retry still tries instead of waiting for it to change. - const ignoreOffline = replacing || retryRequested; + // so an explicit retry, or every OFFLINE_RECHECK_MS, still tries once. + const recheck = + offlineRecheckDue && !replacing && !retryRequested && currentIntent.network === "offline"; + const ignoreOffline = replacing || retryRequested || recheck; if (currentIntent.network === "offline" && !ignoreOffline) { yield* clearLease; yield* setState(offlineState(currentIntent, generation, failureCount + 1, latestFailure)); - const applicationActivated = yield* waitForSignal; + const applicationActivated = yield* waitForRetrySignal(OFFLINE_RECHECK_MS); if (applicationActivated) { resetRetryLadder(); } + offlineRecheckDue = true; continue; } @@ -1233,6 +1238,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( runAttempt(attempt, nextGeneration, latestFailure, pendingRetry, ignoreOffline), ); replacing = false; + offlineRecheckDue = false; const failedProbe = yield* Ref.getAndSet(probeUnanswered, false); if (outcome.established) { generation = nextGeneration; @@ -1252,6 +1258,10 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( } continue; } + // A failed recheck while still reported offline is not a connection failure. + if (recheck && !outcome.established && (yield* Ref.get(intent)).network === "offline") { + continue; + } const attemptSpan: Option.Option = outcome.failure.attemptSpan; const error: ConnectionAttemptError = outcome.failure.error;