fix(background): exit after fatal cleanup

This commit is contained in:
auyua9
2026-05-01 16:04:03 +08:00
parent 606f93e0e1
commit 875fffb7d1
2 changed files with 17 additions and 16 deletions
@@ -3,11 +3,18 @@ import { log } from "../../shared"
type ProcessCleanupSignal = NodeJS.Signals | "beforeExit" | "exit"
type ProcessCleanupErrorEvent = "uncaughtException" | "unhandledRejection"
function scheduleForcedExit(cleanupResult: void | Promise<void>, exitCode: number): void {
function scheduleForcedExit(
cleanupResult: void | Promise<void>,
exitCode: number,
exitAfterCleanup = false,
): void {
process.exitCode = exitCode
const exitTimeout = setTimeout(() => process.exit(), 6000)
void Promise.resolve(cleanupResult).finally(() => {
clearTimeout(exitTimeout)
if (exitAfterCleanup) {
process.exit(exitCode)
}
})
}
@@ -32,7 +39,7 @@ function registerErrorEvent(
): (error: unknown) => void {
const listener = (error: unknown) => {
log(`[background-agent] ${signal} received during shutdown cleanup:`, error)
scheduleForcedExit(handler(error), 1)
scheduleForcedExit(handler(error), 1, true)
}
process.on(signal, listener)
return listener