Skip to content

Commit 6a1010a

Browse files
anandgupta42claude
andcommitted
fix: improve session_end coverage for process.exit() and add dedup guard
Address review comment about beforeExit limitations: - Add process.once("exit") handler to cover process.exit() calls - Add dedup flag to prevent double-firing when finally block also runs - Document why SIGINT/SIGTERM are NOT handled (abort controller already triggers loop exit → finally block → normal session_end) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c0b2617 commit 6a1010a

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

packages/altimate-code/src/session/prompt.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ export namespace SessionPrompt {
303303
const session = await Session.get(sessionID)
304304
await Telemetry.init()
305305
Telemetry.setContext({ sessionId: sessionID, projectId: Instance.project?.id ?? "" })
306-
const beforeExitHandler = () => {
306+
let emergencySessionEndFired = false
307+
const emergencySessionEnd = () => {
308+
if (emergencySessionEndFired) return
309+
emergencySessionEndFired = true
307310
Telemetry.track({
308311
type: "session_end",
309312
timestamp: Date.now(),
@@ -314,7 +317,13 @@ export namespace SessionPrompt {
314317
duration_ms: Date.now() - sessionStartTime,
315318
})
316319
}
317-
process.once("beforeExit", beforeExitHandler)
320+
// beforeExit covers event-loop drain without entering the session loop.
321+
// exit covers process.exit() calls (sync only — track() buffers, flush is best-effort).
322+
// SIGINT/SIGTERM are NOT handled here: the abort controller already triggers
323+
// loop exit → finally block → normal session_end. Adding signal handlers
324+
// would interfere with the default termination behavior.
325+
process.once("beforeExit", emergencySessionEnd)
326+
process.once("exit", emergencySessionEnd)
318327
try {
319328
while (true) {
320329
SessionStatus.set(sessionID, { type: "busy" })
@@ -808,7 +817,8 @@ export namespace SessionPrompt {
808817
}
809818
SessionCompaction.prune({ sessionID })
810819
} finally {
811-
process.removeListener("beforeExit", beforeExitHandler)
820+
process.removeListener("beforeExit", emergencySessionEnd)
821+
process.removeListener("exit", emergencySessionEnd)
812822
const outcome: "completed" | "abandoned" | "error" = abort.aborted
813823
? "abandoned"
814824
: sessionHadError

0 commit comments

Comments
 (0)