TypeScript
fix(web,mobile): time out client activity reports that never settle (#24362)
Sky Hong35c76967906b
diff --git a/apps/mobile/src/connection/background-activity.ts b/apps/mobile/src/connection/background-activity.tsindex 8dec20d284f..66eb2b10e02 100644--- a/apps/mobile/src/connection/background-activity.ts+++ b/apps/mobile/src/connection/background-activity.ts@@ -24,6 +24,8 @@ import {} from "./background-activity-scopes";const REPORT_INTERVAL_MS = 25_000;+// Shorter than the interval, so a report that never settles cannot hold up the next one.+const REPORT_TIMEOUT_MS = 15_000;const LEASE_TTL_MS = 45_000;const BASELINE_SCOPES: ReadonlyArray<BackgroundScope> = [{ type: "provider-status" }];@@ -81,7 +83,8 @@ export const layerReporter = Layer.effectDiscard()// Not `ignore`: tearing down an environment mid-request interrupts its report,// and an interrupt leaking out here would end the reporting loop for good.- .pipe(Effect.ignoreCause),+ // The timeout covers a live connection that never answers, which would stall it.+ .pipe(Effect.timeout(`${REPORT_TIMEOUT_MS} millis`), Effect.ignoreCause),{ concurrency: "unbounded", discard: true },);}).pipe(Effect.withSpan("mobile.backgroundActivity.report"));diff --git a/apps/web/src/lib/backgroundActivityReporter.test.ts b/apps/web/src/lib/backgroundActivityReporter.test.tsindex ae4b980afe0..bbb24ce1b85 100644--- a/apps/web/src/lib/backgroundActivityReporter.test.ts+++ b/apps/web/src/lib/backgroundActivityReporter.test.ts@@ -111,4 +111,29 @@ describe("layer", () => {expect(calls).toBe(2);}).pipe(Effect.scoped),);++ it.effect("keeps reporting after a report never settles", () =>+ Effect.gen(function* () {+ const entries = yield* SubscriptionRef.make(+ new Map([[EnvironmentId.make("environment-reporter-test"), {}]]),+ );+ let calls = 0;+ const registry = EnvironmentRegistry.EnvironmentRegistry.of({+ entries,+ run: () => {+ calls += 1;+ return calls === 1 ? Effect.never : Effect.void;+ },+ } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);++ yield* Layer.build(+ layer.pipe(Layer.provide(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, registry))),+ );+ yield* TestClock.adjust("1 second");+ expect(calls).toBe(1);++ yield* TestClock.adjust("25 seconds");+ expect(calls).toBe(2);+ }).pipe(Effect.scoped),+ );});diff --git a/apps/web/src/lib/backgroundActivityReporter.ts b/apps/web/src/lib/backgroundActivityReporter.tsindex 70b34f9140c..a9f90b17181 100644--- a/apps/web/src/lib/backgroundActivityReporter.ts+++ b/apps/web/src/lib/backgroundActivityReporter.ts@@ -23,6 +23,8 @@ import { randomUUID } from "./utils";const CLIENT_ID_STORAGE_KEY = "t3.backgroundActivity.clientId";const REPORT_INTERVAL_MS = 25_000;+// Shorter than the interval, so a report that never settles cannot hold up the next one.+const REPORT_TIMEOUT_MS = 15_000;const LEASE_TTL_MS = 45_000;const RECENT_INTERACTION_WINDOW_MS = LEASE_TTL_MS;const BASELINE_SCOPES: ReadonlyArray<BackgroundScope> = [{ type: "provider-status" }];@@ -207,7 +209,8 @@ export const layer = Layer.effectDiscard()// Not `ignore`: tearing down an environment mid-request interrupts its report,// and an interrupt leaking out here would end the reporting loop for good.- .pipe(Effect.ignoreCause),+ // The timeout covers a live connection that never answers, which would stall it.+ .pipe(Effect.timeout(`${REPORT_TIMEOUT_MS} millis`), Effect.ignoreCause),{ concurrency: "unbounded", discard: true },);}).pipe(Effect.withSpan("web.backgroundActivity.report"));