Skip to content

Commit 8851e30

Browse files
fix(client-runtime): slow foreground probes keep live sessions connected (pingdotgg#24309)
1 parent a2a3388 commit 8851e30

2 files changed

Lines changed: 117 additions & 2 deletions

File tree

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as Effect from "effect/Effect";
1010
import * as Fiber from "effect/Fiber";
1111
import * as Layer from "effect/Layer";
1212
import * as Option from "effect/Option";
13+
import * as Queue from "effect/Queue";
1314
import * as Random from "effect/Random";
1415
import * as Ref from "effect/Ref";
1516
import * as Stream from "effect/Stream";
@@ -1324,6 +1325,103 @@ describe("EnvironmentSupervisor", () => {
13241325
}),
13251326
);
13261327

1328+
it.effect(
1329+
"keeps a live session when the automatic follow-up to a slow foreground probe answers",
1330+
() =>
1331+
Effect.gen(function* () {
1332+
const probeCount = yield* Ref.make(0);
1333+
const probeStarted = yield* Queue.unbounded<number>();
1334+
const harness = yield* makeHarness({
1335+
probe: () =>
1336+
Ref.updateAndGet(probeCount, (count) => count + 1).pipe(
1337+
Effect.tap((call) => Queue.offer(probeStarted, call)),
1338+
Effect.flatMap((call) => (call === 1 ? Effect.never : Effect.void)),
1339+
),
1340+
});
1341+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1342+
initiallyDesired: true,
1343+
}).pipe(Effect.provide(harness.dependencies));
1344+
1345+
yield* awaitState(
1346+
supervisor.state,
1347+
(state) => state.phase === "connected" && state.generation === 1,
1348+
);
1349+
yield* harness.wake("application-active");
1350+
expect(yield* Queue.take(probeStarted)).toBe(1);
1351+
// A busy server misses the foreground probe deadline once while the
1352+
// socket stays open; the supervisor asks again on its own.
1353+
yield* TestClock.adjust("15 seconds");
1354+
expect(yield* Queue.take(probeStarted)).toBe(2);
1355+
yield* TestClock.adjust("30 seconds");
1356+
1357+
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
1358+
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
1359+
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
1360+
phase: "connected",
1361+
generation: 1,
1362+
});
1363+
}).pipe(Effect.provide(TestClock.layer())),
1364+
);
1365+
1366+
it.effect("replaces a session when the automatic follow-up probe also goes unanswered", () =>
1367+
Effect.gen(function* () {
1368+
const probeCount = yield* Ref.make(0);
1369+
const harness = yield* makeHarness({
1370+
probe: () =>
1371+
Ref.update(probeCount, (count) => count + 1).pipe(Effect.andThen(Effect.never)),
1372+
});
1373+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1374+
initiallyDesired: true,
1375+
}).pipe(Effect.provide(harness.dependencies));
1376+
1377+
yield* awaitState(
1378+
supervisor.state,
1379+
(state) => state.phase === "connected" && state.generation === 1,
1380+
);
1381+
// One foreground event and no other signal: the follow-up's miss alone
1382+
// replaces the session.
1383+
yield* harness.wake("application-active");
1384+
yield* TestClock.adjust("15 seconds");
1385+
yield* TestClock.adjust("15 seconds");
1386+
yield* awaitState(
1387+
supervisor.state,
1388+
(state) => state.phase === "connected" && state.generation === 2,
1389+
);
1390+
1391+
expect(yield* Ref.get(probeCount)).toBe(2);
1392+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1393+
}).pipe(Effect.provide(TestClock.layer())),
1394+
);
1395+
1396+
it.effect("replaces a session when a retry lands late in a slow foreground probe", () =>
1397+
Effect.gen(function* () {
1398+
const probeStarted = yield* Deferred.make<void>();
1399+
const harness = yield* makeHarness({
1400+
probe: () => Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never)),
1401+
});
1402+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
1403+
initiallyDesired: true,
1404+
}).pipe(Effect.provide(harness.dependencies));
1405+
1406+
yield* awaitState(
1407+
supervisor.state,
1408+
(state) => state.phase === "connected" && state.generation === 1,
1409+
);
1410+
yield* harness.wake("application-active");
1411+
yield* Deferred.await(probeStarted);
1412+
yield* TestClock.adjust("13 seconds");
1413+
// The retry's own deadline falls after the foreground one, but it still
1414+
// asks for a definite answer, so the first miss is not forgiven.
1415+
yield* supervisor.retryNow;
1416+
yield* TestClock.adjust("5 seconds");
1417+
yield* awaitState(
1418+
supervisor.state,
1419+
(state) => state.phase === "connected" && state.generation === 2,
1420+
);
1421+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1422+
}).pipe(Effect.provide(TestClock.layer())),
1423+
);
1424+
13271425
it.effect("does not churn a healthy session when credentials change", () =>
13281426
Effect.gen(function* () {
13291427
const harness = yield* makeHarness();

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
691691
}
692692
};
693693

694+
const isForegroundProbe = (next: SupervisorSignal) =>
695+
next._tag === "Wakeup" && next.reason === "application-active";
696+
694697
// Ends "silent" when the host answers a fresh request while the socket
695698
// delivers nothing, as when iOS kills a suspended socket without a close
696699
// event, and "alive" once the socket delivers a frame. A slow or
@@ -772,7 +775,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
772775
continue;
773776
}
774777
yield* Ref.set(probeUnanswered, true);
775-
const probe = yield* Effect.forkChild(lease.session.probe);
778+
let probe = yield* Effect.forkChild(lease.session.probe);
779+
// A busy server can miss one foreground probe while the socket is still
780+
// healthy, so the first miss asks again and only the follow-up's miss
781+
// replaces the session.
782+
let toleratesMiss = isForegroundProbe(next);
776783
let silentSocket: Fiber.Fiber<"silent" | "alive"> | undefined;
777784
// A signal during a running check, such as a network change that kills
778785
// 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* (
785792
// The web and desktop foreground probe allows a busy host 15 seconds,
786793
// and a busy host can answer a fresh request before a queued probe.
787794
const armSilentSocketCheck = Effect.fnUntraced(function* (signal: SupervisorSignal) {
788-
if (signal._tag === "Wakeup" && signal.reason === "application-active") return;
795+
if (isForegroundProbe(signal)) return;
789796
if (silentSocket !== undefined) {
790797
recheck = true;
791798
return;
@@ -837,6 +844,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
837844
return true;
838845
}
839846
if (probeEvent._tag === "TimedOut") {
847+
if (toleratesMiss) {
848+
toleratesMiss = false;
849+
yield* Fiber.interrupt(probe);
850+
probe = yield* Effect.forkChild(lease.session.probe);
851+
deadline = (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(probeTimeout);
852+
continue;
853+
}
840854
yield* stopProbe;
841855
// A long suspension leaves the socket suspect even when the network
842856
// reports offline, so it is replaced like a silent one.
@@ -861,6 +875,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
861875
}
862876
const signalTimeout = probeTimeoutFor(probeEvent.signal, routeKind);
863877
if (signalTimeout !== undefined) {
878+
// A retry or network signal wants a definite answer, even when its
879+
// own deadline lands after the foreground one.
880+
if (!isForegroundProbe(probeEvent.signal)) toleratesMiss = false;
864881
if (isLongResume(probeEvent.signal) && !longResume) {
865882
longResume = true;
866883
longResumeStartedAt = yield* Clock.monotonicTimeNanos;

0 commit comments

Comments
 (0)