Skip to content

Commit fd79bf0

Browse files
committed
fix: gracefully shutdown on CTRL+C from terminal
1 parent b7dff9e commit fd79bf0

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

ts/mains/main_node.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,10 @@ app.on('ready', async () => {
709709
const userDataPath = getRealPath(app.getPath('userData'));
710710
const installPath = getRealPath(join(app.getAppPath(), '..', '..'));
711711

712+
// Register signal handlers
713+
process.on('SIGINT', () => gracefulShutdown('SIGINT')); // Ctrl+C
714+
process.on('SIGTERM', () => gracefulShutdown('SIGTERM')); // Termination request
715+
712716
installFileHandler({
713717
protocol: electronProtocol,
714718
userDataPath,
@@ -859,7 +863,7 @@ async function requestShutdown() {
859863
setTimeout(() => {
860864
console.log('requestShutdown: Response never received; forcing shutdown.');
861865
resolve(undefined);
862-
}, 2 * DURATION.MINUTES);
866+
}, 1 * DURATION.MINUTES);
863867
});
864868

865869
try {
@@ -869,6 +873,13 @@ async function requestShutdown() {
869873
}
870874
}
871875

876+
async function gracefulShutdown(signal: string) {
877+
console.warn(`Received ${signal}, shutting down...`);
878+
await requestShutdown();
879+
app.quit();
880+
console.warn(`Received ${signal}, shutting down: done`);
881+
}
882+
872883
app.on('before-quit', () => {
873884
console.log('before-quit event', {
874885
readyForShutdown: mainWindow ? readyForShutdown : null,

0 commit comments

Comments
 (0)