fix: graceful ctrl+c shutdown and await session-end memory writes

This commit is contained in:
William Valentin
2026-02-18 11:30:47 -08:00
parent 55cde541ea
commit 21232748b9
6 changed files with 103 additions and 30 deletions
+30 -10
View File
@@ -227,15 +227,29 @@ export function registerTuiCommand(program: Command): void {
},
});
const cleanup = () => {
void lifecycle.shutdown();
sessionStore.close();
let cleanupPromise: Promise<void> | null = null;
const cleanup = async () => {
if (!cleanupPromise) {
cleanupPromise = (async () => {
await lifecycle.shutdown();
sessionStore.close();
})();
}
return cleanupPromise;
};
process.on('SIGINT', () => {
cleanup();
process.exit(0);
});
const signalHandler = (signal: NodeJS.Signals) => {
console.log(`\nReceived ${signal}; shutting down TUI...`);
void cleanup()
.then(() => process.exit(0))
.catch((error) => {
console.error('TUI shutdown failed:', error);
process.exit(1);
});
};
process.on('SIGINT', signalHandler);
process.on('SIGTERM', signalHandler);
const transferSessionToTarget = (target: string): string => {
const normalizedTarget = target.trim().toLowerCase();
@@ -274,7 +288,9 @@ export function registerTuiCommand(program: Command): void {
modelProviderConfigs,
contextThresholdPct: config.compaction.threshold_pct,
onTransfer: transferSessionToTarget,
onExit: cleanup,
onExit: () => {
void cleanup();
},
});
} else {
let switchingToFullscreen = false;
@@ -316,12 +332,16 @@ export function registerTuiCommand(program: Command): void {
modelProviderConfigs,
contextThresholdPct: config.compaction.threshold_pct,
onTransfer: transferSessionToTarget,
onExit: cleanup,
onExit: () => {
void cleanup();
},
});
return;
}
}
cleanup();
await cleanup();
process.off('SIGINT', signalHandler);
process.off('SIGTERM', signalHandler);
});
}