fix: graceful ctrl+c shutdown and await session-end memory writes
This commit is contained in:
+30
-10
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user