Skip to content

Commit 47c1c0e

Browse files
skyhong2002claude
andcommitted
fix(web,mobile): time out client activity reports that never settle
A report waits for every environment's request, and the reporting loop runs one report at a time. A request on a live connection that never answers was neither failed nor interrupted, so it held the loop forever and the provider-status lease expired. Time each request out after 15 s, shorter than the 25 s report interval. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4fef307 commit 47c1c0e

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎apps/mobile/src/connection/background-activity.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
} from "./background-activity-scopes";
2525

2626
const REPORT_INTERVAL_MS = 25_000;
27+
// Shorter than the interval, so a report that never settles cannot hold up the next one.
28+
const REPORT_TIMEOUT_MS = 15_000;
2729
const LEASE_TTL_MS = 45_000;
2830
const BASELINE_SCOPES: ReadonlyArray<BackgroundScope> = [{ type: "provider-status" }];
2931

@@ -83,7 +85,8 @@ export const layerReporter = Layer.effectDiscard(
8385
)
8486
// Not `ignore`: tearing down an environment mid-request interrupts its report,
8587
// and an interrupt leaking out here would end the reporting loop for good.
86-
.pipe(Effect.ignoreCause),
88+
// The timeout covers a live connection that never answers, which would stall it.
89+
.pipe(Effect.timeout(`${REPORT_TIMEOUT_MS} millis`), Effect.ignoreCause),
8790
{ concurrency: "unbounded", discard: true },
8891
);
8992
}).pipe(Effect.withSpan("mobile.backgroundActivity.report"));

‎apps/web/src/lib/backgroundActivityReporter.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,29 @@ describe("layer", () => {
111111
expect(calls).toBe(2);
112112
}).pipe(Effect.scoped),
113113
);
114+
115+
it.effect("keeps reporting after a report never settles", () =>
116+
Effect.gen(function* () {
117+
const entries = yield* SubscriptionRef.make(
118+
new Map([[EnvironmentId.make("environment-reporter-test"), {}]]),
119+
);
120+
let calls = 0;
121+
const registry = EnvironmentRegistry.EnvironmentRegistry.of({
122+
entries,
123+
run: () => {
124+
calls += 1;
125+
return calls === 1 ? Effect.never : Effect.void;
126+
},
127+
} as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
128+
129+
yield* Layer.build(
130+
layer.pipe(Layer.provide(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, registry))),
131+
);
132+
yield* TestClock.adjust("1 second");
133+
expect(calls).toBe(1);
134+
135+
yield* TestClock.adjust("25 seconds");
136+
expect(calls).toBe(2);
137+
}).pipe(Effect.scoped),
138+
);
114139
});

‎apps/web/src/lib/backgroundActivityReporter.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { randomUUID } from "./utils";
2323

2424
const CLIENT_ID_STORAGE_KEY = "t3.backgroundActivity.clientId";
2525
const REPORT_INTERVAL_MS = 25_000;
26+
// Shorter than the interval, so a report that never settles cannot hold up the next one.
27+
const REPORT_TIMEOUT_MS = 15_000;
2628
const LEASE_TTL_MS = 45_000;
2729
const RECENT_INTERACTION_WINDOW_MS = LEASE_TTL_MS;
2830
const BASELINE_SCOPES: ReadonlyArray<BackgroundScope> = [{ type: "provider-status" }];
@@ -207,7 +209,8 @@ export const layer = Layer.effectDiscard(
207209
)
208210
// Not `ignore`: tearing down an environment mid-request interrupts its report,
209211
// and an interrupt leaking out here would end the reporting loop for good.
210-
.pipe(Effect.ignoreCause),
212+
// The timeout covers a live connection that never answers, which would stall it.
213+
.pipe(Effect.timeout(`${REPORT_TIMEOUT_MS} millis`), Effect.ignoreCause),
211214
{ concurrency: "unbounded", discard: true },
212215
);
213216
}).pipe(Effect.withSpan("web.backgroundActivity.report"));

0 commit comments

Comments
 (0)