MISC: Fix ghost processes when Electron code crashes too early (#1238)

This commit is contained in:
catloversg 2024-05-03 05:38:39 +07:00 committed by GitHub
parent d4bdb8de2b
commit 5696db2788
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,7 +1,24 @@
/* eslint-disable no-process-exit */ /* eslint-disable no-process-exit */
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
const { app, dialog, BrowserWindow, ipcMain, protocol } = require("electron"); const { app, dialog, BrowserWindow, ipcMain, protocol } = require("electron");
const log = require("electron-log"); const log = require("electron-log");
log.catchErrors();
// This handler must be set ASAP to prevent ghost processes.
process.on("uncaughtException", function () {
// The exception will be logged by electron-log.
app.quit();
process.exit(1);
});
// This handler must be set ASAP to prevent ghost processes.
app.on("window-all-closed", () => {
log.info("Quitting the app...");
app.quit();
process.exit(0);
});
const greenworks = require("./greenworks"); const greenworks = require("./greenworks");
const api = require("./api-server"); const api = require("./api-server");
const gameWindow = require("./gameWindow"); const gameWindow = require("./gameWindow");
@ -17,15 +34,8 @@ const { fileURLToPath } = require("url");
log.transports.file.level = store.get("file-log-level", "info"); log.transports.file.level = store.get("file-log-level", "info");
log.transports.console.level = store.get("console-log-level", "debug"); log.transports.console.level = store.get("console-log-level", "debug");
log.catchErrors();
log.info(`Started app: ${JSON.stringify(process.argv)}`); log.info(`Started app: ${JSON.stringify(process.argv)}`);
process.on("uncaughtException", function () {
// The exception will already have been logged by electron-log
app.quit();
process.exit(1);
});
// We want to fail gracefully if we cannot connect to Steam // We want to fail gracefully if we cannot connect to Steam
try { try {
if (greenworks && greenworks.init()) { if (greenworks && greenworks.init()) {
@ -42,13 +52,6 @@ try {
let isRestoreDisabled = false; let isRestoreDisabled = false;
// This was moved so that startup errors do not lead to ghost processes
app.on("window-all-closed", () => {
log.info("Quitting the app...");
app.quit();
process.exit(0);
});
function setStopProcessHandler(app, window) { function setStopProcessHandler(app, window) {
const closingWindowHandler = async (e) => { const closingWindowHandler = async (e) => {
// We need to prevent the default closing event to add custom logic // We need to prevent the default closing event to add custom logic