From 82168f1b10907a3ff379a449106f4071c80c4e0c Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 21 Feb 2024 14:36:01 -0500 Subject: [PATCH] revert leave services running (#2280) * Revert "Merge pull request #2269 from Chia-Network/cmj.fix-electron-not-closing" This reverts commit bda1eabbbe9916685a78b31547547a14ada99aad, reversing changes made to abb10ab4405dc0f845b912c690594458284ae21e. * Revert "Merge pull request #2268 from Chia-Network/cmj.spawn-detached-daemon" This reverts commit abb10ab4405dc0f845b912c690594458284ae21e, reversing changes made to b8dbb83ee055adf8261999c110b7f499b01ec583. * Revert "Added checkbox for keeping service running background after GUI closes (#670)" This reverts commit 481690a592240048acf3110313e872ebbaea7cb7. --- packages/gui/src/electron/main.tsx | 36 +++------------ packages/gui/src/util/chiaEnvironment.js | 58 +++--------------------- 2 files changed, 13 insertions(+), 81 deletions(-) diff --git a/packages/gui/src/electron/main.tsx b/packages/gui/src/electron/main.tsx index 15526bb437..34138b9c73 100644 --- a/packages/gui/src/electron/main.tsx +++ b/packages/gui/src/electron/main.tsx @@ -478,26 +478,16 @@ if (ensureSingleInstance() && ensureCorrectEnvironment()) { // if (!guessPackaged()) { // mainWindow.webContents.openDevTools(); // } - mainWindow.on('close', async (e) => { + mainWindow.on('close', (e) => { // if the daemon isn't local we aren't going to try to start/stop it if (decidedToClose || !manageDaemonLifetime(NET)) { return; } - if (!mainWindow) { - throw new Error('`mainWindow` is empty'); - } e.preventDefault(); - if (!isClosing) { isClosing = true; - let keepBackgroundRunning: boolean | undefined; - const p = readPrefs(); - if (typeof p.keepBackgroundRunning === 'boolean') { - keepBackgroundRunning = p.keepBackgroundRunning; - } - if (promptOnQuit) { - const choice = await dialog.showMessageBox({ + const choice = dialog.showMessageBoxSync({ type: 'question', buttons: [i18n._(/* i18n */ { id: 'No' }), i18n._(/* i18n */ { id: 'Yes' })], title: i18n._(/* i18n */ { id: 'Confirm' }), @@ -506,36 +496,22 @@ if (ensureSingleInstance() && ensureCorrectEnvironment()) { id: 'Are you sure you want to quit?', } ), - checkboxChecked: keepBackgroundRunning ?? false, - checkboxLabel: i18n._(/* i18n */ { id: 'Keep service running in the background' }), }); - if (keepBackgroundRunning !== choice.checkboxChecked) { - savePrefs({ ...p, keepBackgroundRunning: choice.checkboxChecked }); - } - if (choice.response === 0) { + if (choice === 0) { isClosing = false; return; } - keepBackgroundRunning = choice.checkboxChecked; } isClosing = false; decidedToClose = true; - + mainWindow.webContents.send('exit-daemon'); // save the window state and unmange so we don't restore the mini exiting state mainWindowState.saveState(mainWindow); - mainWindowState.unmanage(); - - if (keepBackgroundRunning) { - mainWindow.close(); - openedWindows.forEach((win) => win.close()); - return; - } - - mainWindow.webContents.send('exit-daemon'); + mainWindowState.unmanage(mainWindow); mainWindow.setBounds({ height: 500, width: 500 }); mainWindow.center(); ipcMain.on('daemon-exited', () => { - mainWindow?.close(); + mainWindow.close(); openedWindows.forEach((win) => win.close()); }); diff --git a/packages/gui/src/util/chiaEnvironment.js b/packages/gui/src/util/chiaEnvironment.js index 9fd1037522..bb10570597 100644 --- a/packages/gui/src/util/chiaEnvironment.js +++ b/packages/gui/src/util/chiaEnvironment.js @@ -67,56 +67,17 @@ const getChiaVersion = () => { return version; }; -const spawnChildProcess = (command, args = [], options = undefined) => { - // As of Feb 11 2024, there is a bug in Electron that prevents electron from exiting when a child process is spawned and detached. - // This is a workaround for that bug. - if (process.platform === 'linux') { - // https://github.com/electron/electron/issues/34808#issuecomment-1275530924 - return childProcess.spawn( - '/bin/bash', - [ - '-c', - 'for fd in $(ls /proc/$$/fd); do case "$fd" in 0|1|2|255) ;; *) eval "exec $fd<&-" ;; esac; done; exec "$@"', - '--', - command, - ...args, - ], - options - ); - } - return childProcess.spawn(command, args, options); -}; - const startChiaDaemon = () => { const script = getScriptPath(PY_DIST_FILE); const processOptions = {}; - if (process.platform === 'win32') { - // We want to detach child daemon process from parent GUI process. - // You may think `detached: true` will do but it shows blank terminal on Windows. - // In order to hide the blank terminal while detaching child process, - // {detached: false, windowsHide: false, shell: true} works which is exact opposite of what we expect - // Please see the comment below for more details. - // https://github.com/nodejs/node/issues/21825#issuecomment-503766781 - processOptions.detached = false; - processOptions.stdio = 'ignore'; - processOptions.windowsHide = false; - processOptions.shell = true; - } else { - processOptions.detached = true; - processOptions.stdio = 'ignore'; - processOptions.windowsHide = true; - } + // processOptions.detached = true; + // processOptions.stdio = "ignore"; pyProc = null; if (guessPackaged()) { try { console.info('Running python executable: '); - if (processOptions.stdio === 'ignore') { - const subProcess = spawnChildProcess(script, ['--wait-for-unlock'], processOptions); - subProcess.unref(); - } else { - const Process = childProcess.spawn; - pyProc = new Process(script, ['--wait-for-unlock'], processOptions); - } + const Process = childProcess.spawn; + pyProc = new Process(script, ['--wait-for-unlock'], processOptions); } catch (e) { console.info('Running python executable: Error: '); console.info(`Script ${script}`); @@ -125,15 +86,10 @@ const startChiaDaemon = () => { console.info('Running python script'); console.info(`Script ${script}`); - if (processOptions.stdio === 'ignore') { - const subProcess = spawnChildProcess('python', [script, '--wait-for-unlock'], processOptions); - subProcess.unref(); - } else { - const Process = childProcess.spawn; - pyProc = new Process('python', [script, '--wait-for-unlock'], processOptions); - } + const Process = childProcess.spawn; + pyProc = new Process('python', [script, '--wait-for-unlock'], processOptions); } - if (pyProc != null && processOptions.stdio !== 'ignore') { + if (pyProc != null) { pyProc.stdout.setEncoding('utf8'); pyProc.stdout.on('data', (data) => {