@devries-cameron20 / t3code Mirror · read-only / commits / ece6322f61
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 04:30 UTC

fix(server): quitting no longer waits on an unreachable telemetry endpoint (#30033)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

levelorbit committed Nov 4, 2026 at 00:44 UTC ece6322f611ae68bf2aa13961124ffa1ba87ee4f
2 files changed +49 -24
apps/server/src/telemetry/AnalyticsService.test.ts
+43 -22
@@ -5,8 +5,11 @@ import { assert, it } from "@effect/vitest";
5 import * as Clock from "effect/Clock";
6 import * as ConfigProvider from "effect/ConfigProvider";
7 import * as Effect from "effect/Effect";
8 +import * as Exit from "effect/Exit";
9 +import * as Fiber from "effect/Fiber";
10 import * as Layer from "effect/Layer";
11 import * as Schema from "effect/Schema";
12 +import * as Scope from "effect/Scope";
13 import * as TestClock from "effect/testing/TestClock";
14 import * as HttpClient from "effect/http/HttpClient";
15 import * as HttpClientError from "effect/http/HttpClientError";
@@ -110,6 +113,30 @@ const recordTwentyEvents = Effect.gen(function* () {
113 }
114 });
115
116 +const analyticsLayerWith = (client: HttpClient.HttpClient, policy?: ManagedPolicyDocument) =>
117 + AnalyticsService.layer.pipe(
118 + Layer.provideMerge(
119 + ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "t3-telemetry-retry-" }),
120 + ),
121 + Layer.provide(
122 + ConfigProvider.layer(
123 + ConfigProvider.fromUnknown({
124 + T3CODE_TELEMETRY_ENABLED: true,
125 + T3CODE_POSTHOG_KEY: "phc_test_key",
126 + T3CODE_POSTHOG_HOST: "http://localhost",
127 + }),
128 + ),
129 + ),
130 + Layer.provide(
131 + Layer.mergeAll(
132 + Layer.succeed(HostProcess.Platform, "win32"),
133 + Layer.succeed(HostProcess.Architecture, "x64"),
134 + Layer.succeed(HttpClient.HttpClient, client),
135 + ManagedPolicy.layerTest(policy),
136 + ),
137 + ),
138 + );
139 +
140 /**
141 * Runs `record` (20 events by default), then lets `minutes` of background
142 * flushes run against a client that answers every send with `respond`.
@@ -138,28 +165,7 @@ const sendFor = (
165 return yield* respond(request);
166 }),
167 );
141 - const runtimeLayer = AnalyticsService.layer.pipe(
142 - Layer.provideMerge(
143 - ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "t3-telemetry-retry-" }),
144 - ),
145 - Layer.provide(
146 - ConfigProvider.layer(
147 - ConfigProvider.fromUnknown({
148 - T3CODE_TELEMETRY_ENABLED: true,
149 - T3CODE_POSTHOG_KEY: "phc_test_key",
150 - T3CODE_POSTHOG_HOST: "http://localhost",
151 - }),
152 - ),
153 - ),
154 - Layer.provide(
155 - Layer.mergeAll(
156 - Layer.succeed(HostProcess.Platform, "win32"),
157 - Layer.succeed(HostProcess.Architecture, "x64"),
158 - Layer.succeed(HttpClient.HttpClient, client),
159 - ManagedPolicy.layerTest(policy),
160 - ),
161 - ),
162 - );
168 + const runtimeLayer = analyticsLayerWith(client, policy);
169
170 yield* Effect.gen(function* () {
171 yield* record;
@@ -271,6 +277,21 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("AnalyticsService
277 }),
278 );
279
280 + it.effect("shutdown stops waiting for a telemetry endpoint that never answers", () =>
281 + Effect.gen(function* () {
282 + const scope = yield* Scope.make();
283 + const context = yield* Layer.buildWithScope(
284 + analyticsLayerWith(HttpClient.make(() => Effect.never)),
285 + scope,
286 + );
287 + yield* recordTwentyEvents.pipe(Effect.provide(context));
288 +
289 + const shutdown = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild);
290 + yield* TestClock.adjust("1 second");
291 + yield* Fiber.join(shutdown);
292 + }),
293 + );
294 +
295 it.effect("flush drains all buffered events across multiple batches", () =>
296 Effect.gen(function* () {
297 const capturedRequests: Array<RecordedBatchRequest> = [];
apps/server/src/telemetry/AnalyticsService.ts
+6 -2
@@ -41,8 +41,10 @@ interface DeliveryState {
41
42 // Retry delays shorter than this still wait for the next background check.
43 const FLUSH_INTERVAL_MS = 10_000;
44 -// A hung send would hold the flush lock, and with it the shutdown flush.
44 +// A hung send would hold the flush lock for the next background flush.
45 const SEND_TIMEOUT = "10 seconds";
46 +// Quit and restart wait for this flush; events it cannot send in time are dropped.
47 +const SHUTDOWN_FLUSH_TIMEOUT = "1 second";
48 // A failed batch is tried again until it has failed for this long. Ingest answers 503 when it
49 // cannot store a batch, and a broken Cloudflare location can take about 5 minutes to fail over.
50 // The waiting batch holds newer events back; `maxBufferedEvents` bounds them meanwhile.
@@ -352,7 +354,9 @@ export const make = Effect.gen(function* () {
354 disableYield: true,
355 }).pipe(Effect.forkScoped);
356
355 - yield* Effect.addFinalizer(() => flush);
357 + yield* Effect.addFinalizer(() =>
358 + flush.pipe(Effect.timeout(SHUTDOWN_FLUSH_TIMEOUT), Effect.ignore),
359 + );
360
361 return AnalyticsService.of({ record, flush, recordOwners });
362 });