Skip to content

Commit 3d42642

Browse files
authored
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.
1 parent 65d67e6 commit 3d42642

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

src/backend/starter.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,29 +194,32 @@ import {
194194
duration: Date.now() - initStartTime,
195195
});
196196

197-
process.on("SIGINT", () => {
198-
systemLogger.info(
199-
"Received SIGINT signal, initiating graceful shutdown...",
200-
{ operation: "shutdown" },
201-
);
197+
const gracefulShutdown = async (signal: string) => {
198+
systemLogger.info(`Received ${signal}, initiating graceful shutdown...`, {
199+
operation: "shutdown",
200+
});
201+
try {
202+
const { saveMemoryDatabaseToFile } = await import(
203+
"./database/db/index.js"
204+
);
205+
await saveMemoryDatabaseToFile();
206+
systemLogger.info("Database saved to disk before exit", {
207+
operation: "shutdown_db_saved",
208+
});
209+
} catch (error) {
210+
systemLogger.error("Failed to save database during shutdown", error, {
211+
operation: "shutdown_db_save_failed",
212+
});
213+
}
202214
process.exit(0);
203-
});
215+
};
204216

205-
process.on("SIGTERM", () => {
206-
systemLogger.info(
207-
"Received SIGTERM signal, initiating graceful shutdown...",
208-
{ operation: "shutdown" },
209-
);
210-
process.exit(0);
211-
});
217+
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
218+
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
212219

213220
process.on("message", (msg: { type?: string }) => {
214221
if (msg?.type === "shutdown") {
215-
systemLogger.info(
216-
"Received IPC shutdown, initiating graceful shutdown...",
217-
{ operation: "shutdown" },
218-
);
219-
process.exit(0);
222+
gracefulShutdown("IPC shutdown");
220223
}
221224
});
222225

0 commit comments

Comments
 (0)