diff --git a/packages/client-runtime/src/connection/supervisor.test.ts b/packages/client-runtime/src/connection/supervisor.test.ts index d6bbae0f5c64d..163075a6efa7f 100644 --- a/packages/client-runtime/src/connection/supervisor.test.ts +++ b/packages/client-runtime/src/connection/supervisor.test.ts @@ -10,6 +10,7 @@ import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; 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"; @@ -1324,6 +1325,103 @@ describe("EnvironmentSupervisor", () => { }), ); + it.effect( + "keeps a live session when the automatic follow-up to a slow foreground probe answers", + () => + Effect.gen(function* () { + const probeCount = yield* Ref.make(0); + const probeStarted = yield* Queue.unbounded(); + const harness = yield* makeHarness({ + probe: () => + Ref.updateAndGet(probeCount, (count) => count + 1).pipe( + Effect.tap((call) => Queue.offer(probeStarted, call)), + Effect.flatMap((call) => (call === 1 ? Effect.never : Effect.void)), + ), + }); + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState( + supervisor.state, + (state) => state.phase === "connected" && state.generation === 1, + ); + yield* harness.wake("application-active"); + expect(yield* Queue.take(probeStarted)).toBe(1); + // A busy server misses the foreground probe deadline once while the + // socket stays open; the supervisor asks again on its own. + yield* TestClock.adjust("15 seconds"); + expect(yield* Queue.take(probeStarted)).toBe(2); + yield* TestClock.adjust("30 seconds"); + + expect(yield* Ref.get(harness.releaseCount)).toBe(0); + expect(yield* Ref.get(harness.sessionCount)).toBe(1); + expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({ + phase: "connected", + generation: 1, + }); + }).pipe(Effect.provide(TestClock.layer())), + ); + + it.effect("replaces a session when the automatic follow-up probe also goes unanswered", () => + Effect.gen(function* () { + const probeCount = yield* Ref.make(0); + const harness = yield* makeHarness({ + probe: () => + Ref.update(probeCount, (count) => count + 1).pipe(Effect.andThen(Effect.never)), + }); + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState( + supervisor.state, + (state) => state.phase === "connected" && state.generation === 1, + ); + // One foreground event and no other signal: the follow-up's miss alone + // replaces the session. + yield* harness.wake("application-active"); + yield* TestClock.adjust("15 seconds"); + yield* TestClock.adjust("15 seconds"); + yield* awaitState( + supervisor.state, + (state) => state.phase === "connected" && state.generation === 2, + ); + + expect(yield* Ref.get(probeCount)).toBe(2); + expect(yield* Ref.get(harness.releaseCount)).toBe(1); + }).pipe(Effect.provide(TestClock.layer())), + ); + + it.effect("replaces a session when a retry lands late in a slow foreground probe", () => + Effect.gen(function* () { + const probeStarted = yield* Deferred.make(); + const harness = yield* makeHarness({ + probe: () => Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never)), + }); + const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, { + initiallyDesired: true, + }).pipe(Effect.provide(harness.dependencies)); + + yield* awaitState( + supervisor.state, + (state) => state.phase === "connected" && state.generation === 1, + ); + yield* harness.wake("application-active"); + yield* Deferred.await(probeStarted); + yield* TestClock.adjust("13 seconds"); + // The retry's own deadline falls after the foreground one, but it still + // asks for a definite answer, so the first miss is not forgiven. + yield* supervisor.retryNow; + yield* TestClock.adjust("5 seconds"); + yield* awaitState( + supervisor.state, + (state) => state.phase === "connected" && state.generation === 2, + ); + expect(yield* Ref.get(harness.releaseCount)).toBe(1); + }).pipe(Effect.provide(TestClock.layer())), + ); + it.effect("does not churn a healthy session when credentials change", () => Effect.gen(function* () { const harness = yield* makeHarness(); diff --git a/packages/client-runtime/src/connection/supervisor.ts b/packages/client-runtime/src/connection/supervisor.ts index 71919917f6209..bbeffd1cb7bc2 100644 --- a/packages/client-runtime/src/connection/supervisor.ts +++ b/packages/client-runtime/src/connection/supervisor.ts @@ -691,6 +691,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( } }; + const isForegroundProbe = (next: SupervisorSignal) => + next._tag === "Wakeup" && next.reason === "application-active"; + // Ends "silent" when the host answers a fresh request while the socket // delivers nothing, as when iOS kills a suspended socket without a close // event, and "alive" once the socket delivers a frame. A slow or @@ -772,7 +775,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( continue; } yield* Ref.set(probeUnanswered, true); - const probe = yield* Effect.forkChild(lease.session.probe); + let probe = yield* Effect.forkChild(lease.session.probe); + // A busy server can miss one foreground probe while the socket is still + // healthy, so the first miss asks again and only the follow-up's miss + // replaces the session. + let toleratesMiss = isForegroundProbe(next); let silentSocket: Fiber.Fiber<"silent" | "alive"> | undefined; // A signal during a running check, such as a network change that kills // the socket, needs a fresh check only if the running one sees a frame. @@ -785,7 +792,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( // The web and desktop foreground probe allows a busy host 15 seconds, // and a busy host can answer a fresh request before a queued probe. const armSilentSocketCheck = Effect.fnUntraced(function* (signal: SupervisorSignal) { - if (signal._tag === "Wakeup" && signal.reason === "application-active") return; + if (isForegroundProbe(signal)) return; if (silentSocket !== undefined) { recheck = true; return; @@ -837,6 +844,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( return true; } if (probeEvent._tag === "TimedOut") { + if (toleratesMiss) { + toleratesMiss = false; + yield* Fiber.interrupt(probe); + probe = yield* Effect.forkChild(lease.session.probe); + deadline = (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(probeTimeout); + continue; + } yield* stopProbe; // A long suspension leaves the socket suspect even when the network // reports offline, so it is replaced like a silent one. @@ -861,6 +875,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* ( } const signalTimeout = probeTimeoutFor(probeEvent.signal, routeKind); if (signalTimeout !== undefined) { + // A retry or network signal wants a definite answer, even when its + // own deadline lands after the foreground one. + if (!isForegroundProbe(probeEvent.signal)) toleratesMiss = false; if (isLongResume(probeEvent.signal) && !longResume) { longResume = true; longResumeStartedAt = yield* Clock.monotonicTimeNanos;