gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Hooks - Fix Telemetry Flush and SessionEnd Hook Execution on Exit

Open Edilmo opened this issue 4 weeks ago • 0 comments

Fix critical production bug where telemetry data and SessionEnd hooks were being silently lost on process exit due to async/sync mismatch in Node.js exit handlers, and add comprehensive integration test coverage for SessionEnd hooks on normal exit.

Node.js's process.on('exit') callback is synchronous and won't wait for promises, but the codebase is calling await shutdownTelemetry(config) inside it. This mean:

  1. SessionEnd hooks never completed - The hook execution starts but is killed when the process exited
  2. Telemetry data was lost - OpenTelemetry SDK shutdown never completes, losing all buffered telemetry
  3. Silent failure - No errors are thrown, making this bug invisible

The root cause is in packages/core/src/telemetry/sdk.ts:202-204:

process.on('exit', () => {
  shutdownTelemetry(config); // ❌ Async function in sync callback!
});

Additionally, packages/cli/src/nonInteractiveCli.ts is shutting down telemetry in its finally block before SessionEnd hooks could fire in runExitCleanup(), causing hooks to be lost in non-interactive mode.

Edilmo avatar Dec 02 '25 01:12 Edilmo