Skip to content

Commit 3eaf43f

Browse files
fix(client-runtime): a missed foreground probe asks again on its own
The first tolerated foreground probe timeout ended the probe without a second attempt, so a session whose transport kept answering pings but whose app RPC stalled stayed connected until the user refocused. The first miss now starts one follow-up probe with the same deadline; its answer keeps the lease and its miss replaces the session. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 012a799 commit 3eaf43f

2 files changed

Lines changed: 55 additions & 64 deletions

File tree

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

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -776,55 +776,50 @@ describe("EnvironmentSupervisor", () => {
776776
}),
777777
);
778778

779-
it.effect("keeps a live session through one slow foreground probe that later answers", () =>
780-
Effect.gen(function* () {
781-
const probeCalls = yield* Ref.make(0);
782-
const firstProbeStarted = yield* Deferred.make<void>();
783-
const secondProbeAnswered = yield* Deferred.make<void>();
784-
const harness = yield* makeHarness({
785-
probe: () =>
786-
Ref.updateAndGet(probeCalls, (count) => count + 1).pipe(
787-
Effect.flatMap((call) =>
788-
call === 1
789-
? Deferred.succeed(firstProbeStarted, undefined).pipe(Effect.andThen(Effect.never))
790-
: Deferred.succeed(secondProbeAnswered, undefined),
779+
it.effect(
780+
"keeps a live session when the automatic follow-up to a slow foreground probe answers",
781+
() =>
782+
Effect.gen(function* () {
783+
const probeCount = yield* Ref.make(0);
784+
const probeStarted = yield* Queue.unbounded<number>();
785+
const harness = yield* makeHarness({
786+
probe: () =>
787+
Ref.updateAndGet(probeCount, (count) => count + 1).pipe(
788+
Effect.tap((call) => Queue.offer(probeStarted, call)),
789+
Effect.flatMap((call) => (call === 1 ? Effect.never : Effect.void)),
791790
),
792-
),
793-
});
794-
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
795-
initiallyDesired: true,
796-
}).pipe(Effect.provide(harness.dependencies));
797-
798-
yield* awaitState(
799-
supervisor.state,
800-
(state) => state.phase === "connected" && state.generation === 1,
801-
);
802-
yield* harness.wake("application-active");
803-
yield* Deferred.await(firstProbeStarted);
804-
// A busy server misses the foreground probe deadline once; the socket
805-
// never closed.
806-
yield* TestClock.adjust("18 seconds");
807-
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
808-
yield* harness.wake("application-active");
809-
yield* Deferred.await(secondProbeAnswered);
810-
811-
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
812-
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
813-
expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(1);
814-
}).pipe(Effect.provide(TestClock.layer())),
791+
});
792+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
793+
initiallyDesired: true,
794+
}).pipe(Effect.provide(harness.dependencies));
795+
796+
yield* awaitState(
797+
supervisor.state,
798+
(state) => state.phase === "connected" && state.generation === 1,
799+
);
800+
yield* harness.wake("application-active");
801+
expect(yield* Queue.take(probeStarted)).toBe(1);
802+
// A busy server misses the foreground probe deadline once while the
803+
// socket stays open; the supervisor asks again on its own.
804+
yield* TestClock.adjust("15 seconds");
805+
expect(yield* Queue.take(probeStarted)).toBe(2);
806+
yield* TestClock.adjust("30 seconds");
807+
808+
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
809+
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
810+
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
811+
phase: "connected",
812+
generation: 1,
813+
});
814+
}).pipe(Effect.provide(TestClock.layer())),
815815
);
816816

817-
it.effect("replaces a session after consecutive unanswered foreground probes", () =>
817+
it.effect("replaces a session when the automatic follow-up probe also goes unanswered", () =>
818818
Effect.gen(function* () {
819-
const probeCalls = yield* Ref.make(0);
820-
const probeStarted = yield* Queue.unbounded<number>();
819+
const probeCount = yield* Ref.make(0);
821820
const harness = yield* makeHarness({
822-
// Misses, answers, then misses twice in a row.
823821
probe: () =>
824-
Ref.updateAndGet(probeCalls, (count) => count + 1).pipe(
825-
Effect.tap((call) => Queue.offer(probeStarted, call)),
826-
Effect.flatMap((call) => (call === 2 ? Effect.void : Effect.never)),
827-
),
822+
Ref.update(probeCount, (count) => count + 1).pipe(Effect.andThen(Effect.never)),
828823
});
829824
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
830825
initiallyDesired: true,
@@ -834,21 +829,17 @@ describe("EnvironmentSupervisor", () => {
834829
supervisor.state,
835830
(state) => state.phase === "connected" && state.generation === 1,
836831
);
837-
for (const attempt of [1, 2, 3]) {
838-
yield* harness.wake("application-active");
839-
expect(yield* Queue.take(probeStarted)).toBe(attempt);
840-
yield* TestClock.adjust("18 seconds");
841-
}
842-
// The answer in between reset the tolerance, so the session survives.
843-
expect(yield* Ref.get(harness.releaseCount)).toBe(0);
844-
832+
// One foreground event and no other signal: the follow-up's miss alone
833+
// replaces the session.
845834
yield* harness.wake("application-active");
846-
expect(yield* Queue.take(probeStarted)).toBe(4);
847-
yield* TestClock.adjust("18 seconds");
835+
yield* TestClock.adjust("15 seconds");
836+
yield* TestClock.adjust("15 seconds");
848837
yield* awaitState(
849838
supervisor.state,
850839
(state) => state.phase === "connected" && state.generation === 2,
851840
);
841+
842+
expect(yield* Ref.get(probeCount)).toBe(2);
852843
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
853844
}).pipe(Effect.provide(TestClock.layer())),
854845
);

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,6 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
608608
Stream.runForEach(() => requestBetterRouteCheck(lease)),
609609
Effect.forkScoped,
610610
);
611-
// A busy server can miss one foreground probe while the socket is still
612-
// healthy, so only a second consecutive miss replaces the session.
613-
let missedForegroundProbe = false;
614611
for (;;) {
615612
const next = yield* takeSignal;
616613
const end = yield* connectedLeaseEnd(next, lease);
@@ -628,8 +625,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
628625
continue;
629626
}
630627
yield* Ref.set(probeUnanswered, true);
631-
const probe = yield* Effect.forkChild(lease.session.probe);
632-
let foreground = isForegroundProbe(next);
628+
let probe = yield* Effect.forkChild(lease.session.probe);
629+
// A busy server can miss one foreground probe while the socket is still
630+
// healthy, so the first miss asks again and only the follow-up's miss
631+
// replaces the session.
632+
let toleratesMiss = isForegroundProbe(next);
633633
let deadline = (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(probeTimeout);
634634
for (;;) {
635635
const remaining = deadline - (yield* Clock.monotonicTimeNanos);
@@ -644,10 +644,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
644644
]);
645645
if (probeEvent._tag === "TimedOut") {
646646
yield* Fiber.interrupt(probe);
647-
if (foreground && !missedForegroundProbe) {
648-
missedForegroundProbe = true;
649-
yield* Ref.set(probeUnanswered, false);
650-
break;
647+
if (toleratesMiss) {
648+
toleratesMiss = false;
649+
probe = yield* Effect.forkChild(lease.session.probe);
650+
deadline = (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(probeTimeout);
651+
continue;
651652
}
652653
return yield* new ConnectionTransientError({
653654
reason: "timeout",
@@ -656,7 +657,6 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
656657
}
657658
if (probeEvent._tag === "ProbeCompleted") {
658659
if (Exit.isSuccess(probeEvent.exit)) {
659-
missedForegroundProbe = false;
660660
yield* Ref.set(probeUnanswered, false);
661661
}
662662
yield* probeEvent.exit;
@@ -671,7 +671,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
671671
if (signalTimeout !== undefined) {
672672
// A retry or network signal wants a definite answer, even when its
673673
// own deadline lands after the foreground one.
674-
if (!isForegroundProbe(probeEvent.signal)) foreground = false;
674+
if (!isForegroundProbe(probeEvent.signal)) toleratesMiss = false;
675675
const signalDeadline =
676676
(yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(signalTimeout);
677677
if (signalDeadline < deadline) deadline = signalDeadline;

0 commit comments

Comments
 (0)