From b62097f7d22b3d84d5567c8633c7c4255263715b Mon Sep 17 00:00:00 2001 From: Batuhan1999 Date: Fri, 23 Oct 2026 12:02:10 +0200 Subject: [PATCH] fix(client-runtime): returning to the app no longer restarts the reconnect backoff for an unreachable host Every application-active wakeup ended the retry wait and reset the retry ladder to its first rung. On desktop that fires on each visibilitychange to visible, so a saved SSH environment whose host no longer resolved retried every few seconds for as long as the app was in use and almost never reached the 5 minute cap. A wakeup still ends the wait and retries at once, but an application-active wakeup (desktop or mobile) now resets the ladder at most once per RETRY_MAX_DELAY_MS. A network change still resets it every time. Refs #15616 Co-Authored-By: Claude Opus 5.5 --- .../src/connection/supervisor.test.ts | 72 +++++++++++++++++++ .../src/connection/supervisor.ts | 43 ++++++----- 2 files changed, 96 insertions(+), 19 deletions(-) diff --git a/packages/client-runtime/src/connection/supervisor.test.ts b/packages/client-runtime/src/connection/supervisor.test.ts index a1acb6f245f51..e0ef1ea9d64d3 100644 --- a/packages/client-runtime/src/connection/supervisor.test.ts +++ b/packages/client-runtime/src/connection/supervisor.test.ts @@ -1,9 +1,11 @@ import { AuthStandardClientScopes, EnvironmentId, type ServerConfig } from "@t3tools/contracts"; import { describe, expect, it } from "@effect/vitest"; +import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Queue from "effect/Queue"; import * as Random from "effect/Random"; import * as Ref from "effect/Ref"; import * as Stream from "effect/Stream"; @@ -615,6 +617,76 @@ describe("EnvironmentSupervisor", () => { }).pipe(Effect.provide(TestClock.layer())), ); + it.effect.each([ + "application-active", + "application-active-probe", + "application-active-reconnect", + ] as const)("retries at once on %s but keeps climbing the ladder", (reason) => + Effect.gen(function* () { + const attempts = yield* Queue.unbounded(); + const harness = yield* makeHarness({ + prepare: (attempt) => + Queue.offer(attempts, attempt).pipe( + Effect.andThen(Effect.fail(transient("Could not resolve hostname."))), + ), + }); + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + const nextBackoffDelay = Effect.gen(function* () { + yield* Queue.take(attempts); + const waiting = yield* awaitState(supervisor.state, (state) => state.phase === "backoff"); + return waiting.retryAt! - (yield* Clock.currentTimeMillis); + }); + + const delays = [yield* nextBackoffDelay]; + for (let wakeup = 0; wakeup < 4; wakeup += 1) { + yield* harness.wake(reason); + // The clock never moves here, so each attempt was started by the wakeup. + delays.push(yield* nextBackoffDelay); + } + expect(delays).toEqual([2_000, 2_000, 4_000, 8_000, 16_000]); + + yield* TestClock.adjust("5 minutes"); + yield* awaitState(supervisor.state, (state) => state.phase === "backoff"); + yield* harness.wake(reason); + const restarted = yield* awaitState( + supervisor.state, + (state) => state.phase === "backoff" && state.attempt === 1, + ); + expect(restarted.retryAt! - (yield* Clock.currentTimeMillis)).toBe(2_000); + }).pipe(Effect.provide(TestClock.layer())), + ); + + it.effect("restarts the retry ladder on every network change", () => + Effect.gen(function* () { + const attempts = yield* Queue.unbounded(); + const harness = yield* makeHarness({ + prepare: (attempt) => + Queue.offer(attempts, attempt).pipe(Effect.andThen(Effect.fail(transient()))), + }); + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + const nextBackoffAttempt = Effect.gen(function* () { + yield* Queue.take(attempts); + const waiting = yield* awaitState(supervisor.state, (state) => state.phase === "backoff"); + return waiting.attempt; + }); + + const ladder = [yield* nextBackoffAttempt]; + for (const reason of [ + "application-active", + "application-active", + "network-changed", + ] as const) { + yield* harness.wake(reason); + ladder.push(yield* nextBackoffAttempt); + } + expect(ladder).toEqual([1, 1, 2, 1]); + }).pipe(Effect.provide(TestClock.layer())), + ); + it.effect("probes the active session without reconnecting on application activation", () => Effect.gen(function* () { const probeCount = yield* Ref.make(0); diff --git a/packages/client-runtime/src/connection/supervisor.ts b/packages/client-runtime/src/connection/supervisor.ts index d500fe6d8347c..bd748a1962a21 100644 --- a/packages/client-runtime/src/connection/supervisor.ts +++ b/packages/client-runtime/src/connection/supervisor.ts @@ -842,20 +842,20 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( }, Effect.ensuring(clearLease)); const waitForRetrySignal = Effect.fnUntraced(function* (delayMs: number) { - // @effect-diagnostics-next-line raceFirstWithSleepToTimeout:off - the sleep is the retry delay (false), not a timeout around the signal loop + // @effect-diagnostics-next-line raceFirstWithSleepToTimeout:off - the sleep is the retry delay, not a timeout around the signal loop return yield* Effect.raceFirst( - Effect.sleep(delayMs).pipe(Effect.as(false)), + Effect.sleep(delayMs).pipe(Effect.as(undefined)), Effect.gen(function* () { for (;;) { const next = yield* Queue.take(signals); switch (next._tag) { case "Wakeup": - return ConnectionWakeups.resetsRetryBackoff(next.reason); + return next.reason; case "ConnectRequested": case "DisconnectRequested": case "RetryRequested": case "NetworkChanged": - return false; + return undefined; case "BetterRouteAvailable": break; } @@ -866,9 +866,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( const waitForSignal = Queue.take(signals).pipe( Effect.repeat({ while: (next) => next._tag === "BetterRouteAvailable" }), - Effect.map( - (next) => next._tag === "Wakeup" && ConnectionWakeups.resetsRetryBackoff(next.reason), - ), + Effect.map((next) => (next._tag === "Wakeup" ? next.reason : undefined)), ); const run = Effect.fnUntraced(function* () { @@ -880,6 +878,22 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( failureCount = 0; pendingRetry = Option.none(); }; + let lastForegroundResetAt = Number.NEGATIVE_INFINITY; + // Returning to the app still ends the wait, but restarts the ladder at most + // once per cap window. A window can become visible many times a minute, + // which would otherwise keep an unreachable host on the shortest delays. + // A network change can make the host reachable, so it always resets. + const resetRetryLadderAfterWakeup = Effect.fnUntraced(function* ( + reason: ConnectionWakeups.ConnectionWakeup | undefined, + ) { + if (reason === undefined || !ConnectionWakeups.resetsRetryBackoff(reason)) return; + if (reason !== "network-changed") { + const now = yield* Clock.currentTimeMillis; + if (now - lastForegroundResetAt < RETRY_MAX_DELAY_MS) return; + lastForegroundResetAt = now; + } + resetRetryLadder(); + }); let replacing = false; for (;;) { @@ -900,10 +914,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( if (currentIntent.network === "offline" && !replacing) { yield* clearLease; yield* setState(offlineState(currentIntent, generation, failureCount + 1, latestFailure)); - const applicationActivated = yield* waitForSignal; - if (applicationActivated) { - resetRetryLadder(); - } + yield* resetRetryLadderAfterWakeup(yield* waitForSignal); continue; } @@ -950,10 +961,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( lastFailure: error, retryAt: null, }); - const applicationActivated = yield* waitForSignal; - if (applicationActivated) { - resetRetryLadder(); - } + yield* resetRetryLadderAfterWakeup(yield* waitForSignal); continue; } @@ -982,10 +990,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( lastFailure: latestFailure, retryAt: (yield* Clock.currentTimeMillis) + delayMs, }); - const applicationActivated = yield* waitForRetrySignal(delayMs); - if (applicationActivated) { - resetRetryLadder(); - } + yield* resetRetryLadderAfterWakeup(yield* waitForRetrySignal(delayMs)); } });