TypeScript
fix(client-runtime): connect to the primary environment while the browser reports offline (#29347)
Julius Marminge1436c4571fa0
diff --git a/packages/client-runtime/src/connection/supervisor.test.ts b/packages/client-runtime/src/connection/supervisor.test.tsindex eede2763291..b4fc6955366 100644--- a/packages/client-runtime/src/connection/supervisor.test.ts+++ b/packages/client-runtime/src/connection/supervisor.test.ts@@ -324,7 +324,7 @@ describe("EnvironmentSupervisor", () => {it.effect("waits while offline and connects immediately when the network returns", () =>Effect.gen(function* () {const harness = yield* makeHarness({ networkStatus: "offline" });- const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {+ const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {initiallyDesired: true,}).pipe(Effect.provide(harness.dependencies));@@ -346,6 +346,54 @@ describe("EnvironmentSupervisor", () => {}),);+ it.effect("connects to the primary environment while the device reports offline", () =>+ Effect.gen(function* () {+ const harness = yield* makeHarness({ networkStatus: "offline" });+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {+ initiallyDesired: true,+ }).pipe(Effect.provide(harness.dependencies));++ const settled = yield* awaitState(+ supervisor.state,+ (state) => state.phase === "connected" || state.phase === "offline",+ );++ expect(settled).toMatchObject({+ phase: "connected",+ network: "unknown",+ attempt: 1,+ lastFailure: null,+ });+ expect(yield* Ref.get(harness.prepareCount)).toBe(1);+ }),+ );++ it.effect(+ "keeps a primary attempt through an offline report and restarts it when the network returns",+ () =>+ Effect.gen(function* () {+ const started = yield* Deferred.make<void>();+ const harness = yield* makeHarness({+ prepare: (attempt) =>+ attempt === 1+ ? Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never))+ : Effect.succeed(PREPARED_CONNECTION),+ });+ const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {+ initiallyDesired: true,+ }).pipe(Effect.provide(harness.dependencies));++ yield* Deferred.await(started);+ yield* harness.setNetworkStatus("offline");+ yield* TestClock.adjust("1 second");+ expect(yield* Ref.get(harness.prepareCount)).toBe(1);++ yield* harness.setNetworkStatus("online");+ yield* awaitState(supervisor.state, (state) => state.phase === "connected");+ expect(yield* Ref.get(harness.prepareCount)).toBe(2);+ }).pipe(Effect.provide(TestClock.layer())),+ );+it.effect("keeps the latest failure visible throughout the next connection attempt", () =>Effect.gen(function* () {const harness = yield* makeHarness({@@ -763,6 +811,29 @@ describe("EnvironmentSupervisor", () => {}).pipe(Effect.provide(TestClock.layer())),);+ it.effect("checks the primary environment's socket when the device reports offline", () =>+ Effect.gen(function* () {+ const harness = yield* makeSilentSocketHarness();+ const target = new PrimaryConnectionTarget({+ ...TARGET,+ httpBaseUrl: "http://127.0.0.1:3773",+ wsBaseUrl: "ws://127.0.0.1:3773",+ });+ const supervisor = yield* EnvironmentSupervisor.make(+ { ...TARGET_ENTRY, target },+ { initiallyDesired: true },+ ).pipe(Effect.provide(harness.dependencies));++ yield* awaitState(supervisor.state, (state) => state.phase === "connected");+ yield* harness.setNetworkStatus("offline");+ yield* TestClock.adjust("500 millis");+ yield* awaitState(+ supervisor.state,+ (state) => state.phase === "connected" && state.generation === 2,+ );+ }).pipe(Effect.provide(TestClock.layer())),+ );+it.effect("replaces a remote socket after a long resume while the network reports offline", () =>Effect.gen(function* () {const harness = yield* makeSilentSocketHarness();@@ -971,7 +1042,7 @@ describe("EnvironmentSupervisor", () => {const harness = yield* makeHarness({probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),});- const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {+ const supervisor = yield* EnvironmentSupervisor.make(LAN_ENTRY, {initiallyDesired: true,}).pipe(Effect.provide(harness.dependencies));diff --git a/packages/client-runtime/src/connection/supervisor.ts b/packages/client-runtime/src/connection/supervisor.tsindex 3c969f02062..5f279edf568 100644--- a/packages/client-runtime/src/connection/supervisor.ts+++ b/packages/client-runtime/src/connection/supervisor.ts@@ -288,9 +288,14 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (const connectivity = yield* Connectivity.Connectivity;const driver = yield* ConnectionDriver.ConnectionDriver;const wakeups = yield* ConnectionWakeups.ConnectionWakeups;+ // The primary environment served this client or runs beside it on loopback,+ // so the device reporting offline (often wrongly) never parks it. The report+ // still probes its live session, like any other network change.+ const targetNetwork = (network: NetworkStatus): NetworkStatus =>+ network === "offline" && target._tag === "PrimaryConnectionTarget" ? "unknown" : network;const initialIntent: SupervisorIntent = {desired: options?.initiallyDesired ?? false,- network: yield* connectivity.status,+ network: targetNetwork(yield* connectivity.status),};const intent = yield* Ref.make(initialIntent);const signals = yield* Queue.unbounded<SupervisorSignal>();@@ -570,6 +575,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (});const waitForEstablishmentInterrupt = Effect.fnUntraced(function* () {+ // A primary attempt keeps running through an offline report, so the+ // network returning restarts it in case it stalled meanwhile.+ let deviceOffline = (yield* connectivity.status) === "offline";for (;;) {const next = yield* Queue.take(signals);switch (next._tag) {@@ -577,8 +585,14 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (case "RetryRequested":return false;case "NetworkChanged":- if (next.network === "offline") {- return false;+ if (target._tag !== "PrimaryConnectionTarget") {+ if (next.network === "offline") {+ return false;+ }+ } else if (next.network === "offline") {+ deviceOffline = true;+ } else if (deviceOffline) {+ return true;}break;case "ConnectRequested":@@ -1169,9 +1183,12 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (yield* connectivity.changes.pipe(Stream.runForEach((network) =>- Ref.modify(intent, (current) =>- current.network === network ? [false, current] : ([true, { ...current, network }] as const),- ).pipe(+ Ref.modify(intent, (current) => {+ const next = targetNetwork(network);+ return current.network === next+ ? [false, current]+ : ([true, { ...current, network: next }] as const);+ }).pipe(Effect.flatMap((changed) =>changed? (network === "offline" ? Effect.void : forgetSilentRoutes).pipe(