Skip to content

fix: flush telemetry events before process exit#46

Merged
anandgupta42 merged 2 commits intomainfrom
fix/telemetry-flush-on-exit
Mar 5, 2026
Merged

fix: flush telemetry events before process exit#46
anandgupta42 merged 2 commits intomainfrom
fix/telemetry-flush-on-exit

Conversation

@anandgupta42
Copy link
Contributor

Summary

  • Add Telemetry.shutdown() in the CLI entry point's finally block (index.ts) so buffered events are flushed before process.exit() — previously only the TUI session path called shutdown, causing all non-session commands (auth, upgrade, mcp, etc.) to silently lose their telemetry events
  • Make shutdown() await the in-flight init() promise (telemetry/index.ts) — since init() is called fire-and-forget in CLI middleware, shutdown() could run before init completed, seeing enabled=false and skipping the flush entirely

Test plan

  • Existing telemetry unit tests pass (27/27)
  • Verified via test-telemetry-debug.ts that events are accepted by App Insights (HTTP 200, itemsAccepted: 1)
  • Verified shutdown race condition: fire-and-forget init() + immediate shutdown() correctly flushes all buffered events
  • Verified idempotent shutdown: calling shutdown() twice is safe (first flushes, second is no-op)
  • Verify events appear in Azure App Insights dashboard after deploying

🤖 Generated with Claude Code

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>
@anandgupta42 anandgupta42 requested a review from kulvirgit March 5, 2026 04:37
Copy link

@jontsai jontsai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #46 Review Tour Guide

fix: flush telemetry events before process exit

# File +/- Purpose
1 packages/altimate-code/src/index.ts +9 Add Telemetry.shutdown() in the CLI finally block
2 packages/altimate-code/src/telemetry/index.ts +10 Make shutdown() await the in-flight init() promise

Summary

Solid fix for a real bug — telemetry events from non-session CLI commands (auth, upgrade, mcp, etc.) were silently lost because shutdown() was only called from the session prompt path. Additionally, the fire-and-forget init() could still be in-flight when shutdown() ran, causing it to see enabled=false and skip the flush.

Both changes are purely additive (+19/-0), defensive, and idempotent. Error handling is correct — telemetry failures are swallowed so they never block process exit. The initPromise await in shutdown() neatly closes the race condition.

No issues found. Clean PR, well-documented commit message and test plan. LGTM.

// Wait for init to complete so we know whether telemetry is enabled
// and have a valid endpoint to flush to. init() is fire-and-forget
// in CLI middleware, so it may still be in-flight when shutdown runs.
if (initPromise) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Nice defensive pattern — awaiting the in-flight initPromise before checking enabled state closes the race condition cleanly. The catch swallow is correct here since a failed init means there's nothing to flush.

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>
@anandgupta42 anandgupta42 merged commit 1b64041 into main Mar 5, 2026
5 checks passed
anandgupta42 added a commit that referenced this pull request Mar 5, 2026
- Security FAQ: document well-known URL auth confirmation prompt and
  command validation (from #45)
- Troubleshooting: add MCP server initialization failure section — init
  errors are now logged instead of silently swallowed (from #45)
- Telemetry: document flush retry and flush-before-exit behavior
  (from #45, #46)
- Getting started: mention postinstall welcome banner and upgrade
  changelog display (from #48)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
anandgupta42 added a commit that referenced this pull request Mar 5, 2026
- Security FAQ: document well-known URL auth confirmation prompt and
  command validation (from #45)
- Troubleshooting: add MCP server initialization failure section — init
  errors are now logged instead of silently swallowed (from #45)
- Telemetry: document flush retry and flush-before-exit behavior
  (from #45, #46)
- Getting started: mention postinstall welcome banner and upgrade
  changelog display (from #48)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@kulvirgit kulvirgit deleted the fix/telemetry-flush-on-exit branch March 10, 2026 21:06
anandgupta42 added a commit that referenced this pull request Mar 17, 2026
* 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>
anandgupta42 added a commit that referenced this pull request Mar 17, 2026
- Security FAQ: document well-known URL auth confirmation prompt and
  command validation (from #45)
- Troubleshooting: add MCP server initialization failure section — init
  errors are now logged instead of silently swallowed (from #45)
- Telemetry: document flush retry and flush-before-exit behavior
  (from #45, #46)
- Getting started: mention postinstall welcome banner and upgrade
  changelog display (from #48)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants