fix(#2822): properly cleanup tmux sessions on process shutdown

Two issues fixed:
1. process-cleanup.ts used fire-and-forget void Promise for shutdown
   handlers — now properly collects and awaits all cleanup promises
   via Promise.allSettled, with dedup guard to prevent double cleanup
2. TmuxSessionManager was never registered for process cleanup —
   now registered in create-managers.ts via registerManagerForCleanup

Also fixed setTimeout().unref() which could let the process exit
before cleanup completes.
This commit is contained in:
YeonGyu-Kim
2026-03-27 15:23:48 +09:00
parent 5765168af4
commit 127626a122
2 changed files with 23 additions and 4 deletions
@@ -11,7 +11,7 @@ function registerProcessSignal(
handler()
if (exitAfter) {
process.exitCode = 0
setTimeout(() => process.exit(), 6000).unref()
setTimeout(() => process.exit(), 6000)
}
}
process.on(signal, listener)
@@ -32,16 +32,26 @@ export function registerManagerForCleanup(manager: CleanupTarget): void {
if (cleanupRegistered) return
cleanupRegistered = true
let cleanupPromise: Promise<void> | undefined
const cleanupAll = () => {
if (cleanupPromise) return
const promises: Promise<void>[] = []
for (const m of cleanupManagers) {
try {
void Promise.resolve(m.shutdown()).catch((error) => {
log("[background-agent] Error during async shutdown cleanup:", error)
})
promises.push(
Promise.resolve(m.shutdown()).catch((error) => {
log("[background-agent] Error during async shutdown cleanup:", error)
})
)
} catch (error) {
log("[background-agent] Error during shutdown cleanup:", error)
}
}
cleanupPromise = Promise.allSettled(promises).then(() => {})
cleanupPromise.then(() => {
log("[background-agent] All shutdown cleanup completed")
})
}
const registerSignal = (signal: ProcessCleanupEvent, exitAfter: boolean): void => {