Skip to content

Commit 1b64041

Browse files
anandgupta42claude
andauthored
fix: flush telemetry events before process exit (#46)
* fix: flush telemetry events before process exit Telemetry events were silently lost because: 1. Telemetry.shutdown() was only called from the session prompt flow (prompt.ts). Non-session commands (auth, upgrade, mcp, etc.) tracked events but never flushed them before process.exit(). 2. shutdown() didn't await the in-flight init() promise. Since init() is called fire-and-forget in CLI middleware, shutdown() would see enabled=false and skip the flush entirely. Fix: add Telemetry.shutdown() in the index.ts finally block (before process.exit) and make shutdown() await initPromise so buffered events are flushed even when init hasn't completed yet. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: flush telemetry in TUI worker shutdown The TUI worker subprocess calls Telemetry.init() but its shutdown() handler never called Telemetry.shutdown(), causing events tracked in the worker (MCP server status, engine events) to be lost when the worker exits. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 26fda3b commit 1b64041

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

packages/altimate-code/src/cli/cmd/tui/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export const rpc = {
148148
}),
149149
])
150150
if (server) server.stop(true)
151+
await Telemetry.shutdown()
151152
},
152153
}
153154

packages/altimate-code/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ try {
200200
}
201201
process.exitCode = 1
202202
} finally {
203+
// Flush any buffered telemetry events before exiting.
204+
// This is critical for non-session commands (auth, upgrade, mcp, etc.)
205+
// that track events but don't go through the session prompt shutdown path.
206+
// shutdown() is idempotent — safe even if session prompt already called it.
207+
try {
208+
await Telemetry.shutdown()
209+
} catch {
210+
// Telemetry failure must never prevent shutdown
211+
}
203212
// Some subprocesses don't react properly to SIGTERM and similar signals.
204213
// Most notably, some docker-container-based MCP servers don't handle such signals unless
205214
// run using `docker run --init`.

packages/altimate-code/src/telemetry/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,16 @@ export namespace Telemetry {
481481
}
482482

483483
export async function shutdown() {
484+
// Wait for init to complete so we know whether telemetry is enabled
485+
// and have a valid endpoint to flush to. init() is fire-and-forget
486+
// in CLI middleware, so it may still be in-flight when shutdown runs.
487+
if (initPromise) {
488+
try {
489+
await initPromise
490+
} catch {
491+
// init failed — nothing to flush
492+
}
493+
}
484494
if (flushTimer) {
485495
clearInterval(flushTimer)
486496
flushTimer = undefined

0 commit comments

Comments
 (0)