gemini-cli
gemini-cli copied to clipboard
Hooks - Fix Telemetry Flush and SessionEnd Hook Execution on Exit
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:
- SessionEnd hooks never completed - The hook execution starts but is killed when the process exited
- Telemetry data was lost - OpenTelemetry SDK shutdown never completes, losing all buffered telemetry
- 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.