Skip to content

Commit b40319a

Browse files
fix(client-runtime): connect to the primary environment while the device reports offline
The supervisor gated every target on the browser's network report, so a wrong `navigator.onLine === false` parked the primary environment (the server that served the page, or desktop's loopback backend) in offline, Reconnect did nothing, and the diagnosis blamed the device. The primary target's intent now reads an offline report as unknown, so it connects and retries regardless, while the report itself still probes its live session as before. Relay, bearer and SSH targets keep waiting while offline. Fixes #29323 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 58d1c1c commit b40319a

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,29 @@ describe("EnvironmentSupervisor", () => {
768768
}).pipe(Effect.provide(TestClock.layer())),
769769
);
770770

771+
it.effect("checks the primary environment's socket when the device reports offline", () =>
772+
Effect.gen(function* () {
773+
const harness = yield* makeSilentSocketHarness();
774+
const target = new PrimaryConnectionTarget({
775+
...TARGET,
776+
httpBaseUrl: "http://127.0.0.1:3773",
777+
wsBaseUrl: "ws://127.0.0.1:3773",
778+
});
779+
const supervisor = yield* EnvironmentSupervisor.make(
780+
{ ...TARGET_ENTRY, target },
781+
{ initiallyDesired: true },
782+
).pipe(Effect.provide(harness.dependencies));
783+
784+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
785+
yield* harness.setNetworkStatus("offline");
786+
yield* TestClock.adjust("500 millis");
787+
yield* awaitState(
788+
supervisor.state,
789+
(state) => state.phase === "connected" && state.generation === 2,
790+
);
791+
}).pipe(Effect.provide(TestClock.layer())),
792+
);
793+
771794
it.effect("replaces a remote socket after a long resume while the network reports offline", () =>
772795
Effect.gen(function* () {
773796
const harness = yield* makeSilentSocketHarness();

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,14 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
283283
const connectivity = yield* Connectivity.Connectivity;
284284
const driver = yield* ConnectionDriver.ConnectionDriver;
285285
const wakeups = yield* ConnectionWakeups.ConnectionWakeups;
286+
// The primary environment served this client, so the device reporting
287+
// offline (often wrongly) never parks it. The report still probes its live
288+
// session, like any other network change.
289+
const targetNetwork = (network: NetworkStatus): NetworkStatus =>
290+
network === "offline" && target._tag === "PrimaryConnectionTarget" ? "unknown" : network;
286291
const initialIntent: SupervisorIntent = {
287292
desired: options?.initiallyDesired ?? false,
288-
network: yield* connectivity.status,
293+
network: targetNetwork(yield* connectivity.status),
289294
};
290295
const intent = yield* Ref.make(initialIntent);
291296
const signals = yield* Queue.unbounded<SupervisorSignal>();
@@ -1152,9 +1157,12 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
11521157

11531158
yield* connectivity.changes.pipe(
11541159
Stream.runForEach((network) =>
1155-
Ref.modify(intent, (current) =>
1156-
current.network === network ? [false, current] : ([true, { ...current, network }] as const),
1157-
).pipe(
1160+
Ref.modify(intent, (current) => {
1161+
const next = targetNetwork(network);
1162+
return current.network === next
1163+
? [false, current]
1164+
: ([true, { ...current, network: next }] as const);
1165+
}).pipe(
11581166
Effect.flatMap((changed) =>
11591167
changed
11601168
? (network === "offline" ? Effect.void : forgetSilentRoutes).pipe(

0 commit comments

Comments
 (0)