Skip to content

Commit d540d0f

Browse files
committed
fix: elefctron app wont exit with non-0 exit codes
1 parent 2bbb474 commit d540d0f

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src-electron/main.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,26 @@ async function createWindow() {
137137
win.loadURL(phoenixLoadURL);
138138
}
139139

140+
let isShuttingDown = false;
141+
let pendingExitCode = 0;
142+
140143
async function gracefulShutdown(exitCode = 0) {
144+
// If non-zero exit code, always update (failure takes precedence)
145+
if (exitCode !== 0) {
146+
pendingExitCode = exitCode;
147+
}
148+
if (isShuttingDown) {
149+
return; // Already shutting down
150+
}
151+
isShuttingDown = true;
141152
console.log('Initiating graceful shutdown...');
142-
await terminateAllProcesses();
143-
app.exit(exitCode);
153+
try {
154+
await terminateAllProcesses();
155+
app.exit(pendingExitCode);
156+
} catch (err) {
157+
console.error('Error during shutdown:', err);
158+
app.exit(pendingExitCode || 1);
159+
}
144160
}
145161

146162
// Register all IPC handlers
@@ -314,6 +330,6 @@ app.on('activate', () => {
314330
}
315331
});
316332

317-
// Handle process termination signals
318-
process.on('SIGINT', () => gracefulShutdown(0));
319-
process.on('SIGTERM', () => gracefulShutdown(0));
333+
// Handle process termination signals (exit 128+signal per Unix convention)
334+
process.on('SIGINT', () => gracefulShutdown(130)); // 128 + 2
335+
process.on('SIGTERM', () => gracefulShutdown(143)); // 128 + 15

0 commit comments

Comments
 (0)