Skip to content

fix: drain telemetry on shutdown - #573

Open
Soumyajit2288 wants to merge 1 commit into
wonderwhy-er:mainfrom
Soumyajit2288:agent/flush-telemetry-cleanup
Open

fix: drain telemetry on shutdown#573
Soumyajit2288 wants to merge 1 commit into
wonderwhy-er:mainfrom
Soumyajit2288:agent/flush-telemetry-cleanup

Conversation

@Soumyajit2288

@Soumyajit2288 Soumyajit2288 commented Jul 11, 2026

Copy link
Copy Markdown

Summary

  • track in-flight telemetry sends while keeping normal capture calls fire-and-forget
  • expose flushTelemetry() with a bounded two-second default timeout
  • drain pending sends during beforeExit
  • remove the unused legacy captureBase GA4 transport and its duplicated property-building path

Root cause

The live capture() path detached its async send without retaining the promise, so events emitted immediately before process exit could be dropped. The same module also retained a separate 200-line telemetry implementation with no callers.

Impact

Short-lived shutdown paths now get a bounded opportunity to finish telemetry without delaying normal tool calls or allowing telemetry failures to affect functionality. Removing the dead transport leaves the existing proxy path as the single implementation.

Fixes #504.
Fixes #505.

Validation

  • npm run build
  • node test/test-telemetry-handling.js
  • git diff --check

Summary by CodeRabbit

  • Reliability Improvements
    • Improved telemetry delivery by tracking in-progress sends.
    • Added graceful draining of pending telemetry during application shutdown.
    • Reduced the likelihood that recent telemetry events are lost when the process exits.

@Soumyajit2288

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b353ac18-dae3-4aed-a545-ca4e89f74006

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

captureBase is removed from the telemetry module. capture now tracks asynchronous sends, and exported flushTelemetry drains pending sends with a timeout during process.beforeExit.

Changes

Telemetry lifecycle

Layer / File(s) Summary
Shared event sending
src/utils/capture.ts
Removes the obsolete captureBase section and documents the shared event-property builder used by the telemetry proxy path.
Shutdown telemetry draining
src/utils/capture.ts
Tracks in-flight capture sends, exposes flushTelemetry, removes settled promises, and invokes draining from the beforeExit handler.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Process
  participant Capture
  participant TelemetryProxy
  Process->>Capture: capture(event, properties)
  Capture->>TelemetryProxy: send tracked telemetry
  Process->>Capture: beforeExit
  Capture->>Capture: flushTelemetry(timeout)
  Capture-->>Process: settle pending sends or timeout
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: draining telemetry on shutdown.
Linked Issues check ✅ Passed The changes satisfy #504 and #505 by removing captureBase and adding pending-send flushing on beforeExit.
Out of Scope Changes check ✅ Passed The diff stays within telemetry shutdown handling and dead-code removal; no unrelated changes are evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/utils/capture.ts`:
- Around line 280-282: Replace the beforeExit-only telemetry flush in the
shutdown handling with awaited flushTelemetry() calls in the SIGINT, SIGTERM,
uncaughtException, and unhandledRejection handlers, ensuring each handler
flushes before completing its exit or error path. Remove the beforeExit
registration if it is no longer needed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3930c701-7aed-49b6-bc94-f9e5751f669c

📥 Commits

Reviewing files that changed from the base of the PR and between 78100e6 and 2c17375.

📒 Files selected for processing (1)
  • src/utils/capture.ts

Comment thread src/utils/capture.ts
Comment on lines +280 to +282
process.once('beforeExit', () => {
void flushTelemetry();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find explicit exits, signal handlers, and any flushTelemetry usage.
rg -nP "process\.(exit|on(ce)?\s*\(\s*['\"](SIGINT|SIGTERM|exit|beforeExit))" -g '!**/node_modules/**'
echo '--- flushTelemetry usages ---'
rg -nP '\bflushTelemetry\b' -g '!**/node_modules/**'

Repository: wonderwhy-er/DesktopCommanderMCP

Length of output: 170


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the relevant file and search for shutdown/telemetry wiring.
ast-grep outline src/utils/capture.ts --view expanded || true
echo '--- capture.ts excerpt ---'
sed -n '240,340p' src/utils/capture.ts

echo '--- shutdown/telemetry references ---'
rg -n --hidden --glob '!**/node_modules/**' --glob '!**/dist/**' --glob '!**/build/**' \
  "beforeExit|SIGINT|SIGTERM|uncaughtException|process\.exit|flushTelemetry|sendToTelemetryProxy|pendingCaptures" \
  src .

Repository: wonderwhy-er/DesktopCommanderMCP

Length of output: 22674


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find the top-level flow around flushTelemetry to see whether it is awaited elsewhere.
rg -n --hidden --glob '!**/node_modules/**' --glob '!**/dist/**' --glob '!**/build/**' \
  "\bflushTelemetry\b|\bprocess\.once\b|\bprocess\.on\b|\bprocess\.exit\b" src test . || true

Repository: wonderwhy-er/DesktopCommanderMCP

Length of output: 23827


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# List candidate files that may wire shutdown handlers.
git ls-files | rg '(^|/)(capture|telemetry|signal|shutdown|process).*\.(ts|js|mjs|cjs)$|package\.json$|README\.md$'

Repository: wonderwhy-er/DesktopCommanderMCP

Length of output: 377


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read the relevant section of capture.ts and locate the definitions it depends on.
wc -l src/utils/capture.ts
echo '--- capture.ts (260-330) ---'
sed -n '260,330p' src/utils/capture.ts

echo '--- nearby symbol definitions ---'
rg -n "function flushTelemetry|const flushTelemetry|let pendingCaptures|sendToTelemetryProxy" src/utils/capture.ts

Repository: wonderwhy-er/DesktopCommanderMCP

Length of output: 2812


Flush telemetry on the real shutdown paths
beforeExit won’t run for the direct process.exit() and signal-driven exits in this codebase, so shutdown telemetry can still be dropped. Call and await flushTelemetry() from the SIGINT/SIGTERM/uncaughtException/unhandledRejection handlers instead.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utils/capture.ts` around lines 280 - 282, Replace the beforeExit-only
telemetry flush in the shutdown handling with awaited flushTelemetry() calls in
the SIGINT, SIGTERM, uncaughtException, and unhandledRejection handlers,
ensuring each handler flushes before completing its exit or error path. Remove
the beforeExit registration if it is no longer needed.

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.

Flush pending telemetry before process exit Remove dead captureBase

1 participant