fix: flush in-memory database to disk on graceful shutdown (#792)

SIGTERM/SIGINT handlers previously called process.exit(0) immediately
without persisting the in-memory SQLite database. This caused session
data loss on container restart, forcing users to re-authenticate.
This commit is contained in:
ZacharyZcR
2026-05-18 05:30:13 +08:00
committed by GitHub
parent 65d67e6f79
commit 3d42642e94
+21 -18
View File
@@ -194,29 +194,32 @@ import {
duration: Date.now() - initStartTime,
});
process.on("SIGINT", () => {
systemLogger.info(
"Received SIGINT signal, initiating graceful shutdown...",
{ operation: "shutdown" },
);
const gracefulShutdown = async (signal: string) => {
systemLogger.info(`Received ${signal}, initiating graceful shutdown...`, {
operation: "shutdown",
});
try {
const { saveMemoryDatabaseToFile } = await import(
"./database/db/index.js"
);
await saveMemoryDatabaseToFile();
systemLogger.info("Database saved to disk before exit", {
operation: "shutdown_db_saved",
});
} catch (error) {
systemLogger.error("Failed to save database during shutdown", error, {
operation: "shutdown_db_save_failed",
});
}
process.exit(0);
});
};
process.on("SIGTERM", () => {
systemLogger.info(
"Received SIGTERM signal, initiating graceful shutdown...",
{ operation: "shutdown" },
);
process.exit(0);
});
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("message", (msg: { type?: string }) => {
if (msg?.type === "shutdown") {
systemLogger.info(
"Received IPC shutdown, initiating graceful shutdown...",
{ operation: "shutdown" },
);
process.exit(0);
gracefulShutdown("IPC shutdown");
}
});