即使成功触发了所有退出事件,Electron 应用程序仍挂起退出

Har*_*oel 7 freeze electron electron-builder

一段时间以来,我一直在尝试调试我们的电子应用程序的问题,但这正成为一个难题。

问题:间歇性地,当使用退出快捷方式 ( cmd+Q ) 时,应用程序 UI 成功消失,但它停留在菜单栏 (mac)/通知 try(win) 中,并在悬停时显示死亡沙滩球。

而且我必须去活动监视器来实际强制关闭它 应用程序没有响应

这只发生在打包的生产应用程序中,而不是在开发模式下。在查看日志时,似乎所有退出事件都被正确命中。

app.on('window-all-closed', () => {
  log.info('All windows closed');

  if (process.platform != 'darwin') {
    app.quit();
  }
});
// Logs to help nail down quit failures
app.on('will-quit', () => log.info('App will quit'));
app.on('quit', () => log.info('App is quitting'));

app.on('before-quit', async () => {
 log.info('Attempting to quit app');
}
Run Code Online (Sandbox Code Playgroud)

log.log 的尾部

[2019-02-04 14:29:34.543] [info] electron: message received: windows-info
[2019-02-04 14:29:35.831] [info] Attempting to quit app
[2019-02-04 14:29:35.919] [info] stopping mouse tracking
[2019-02-04 14:29:36.073] [info] App will quit
[2019-02-04 14:29:36.079] [info] App is quitting
[2019-02-04 14:29:36.127] [info]  mac binary: main.swift:49 : sendPayload(messageToSend:): {"type":"will-close-expectedly"}

[2019-02-04 14:29:36.128] [info] electron: message received: will-close-expectedly
[2019-02-04 14:29:36.130] [info] daemon binary /Applications/Loom.app/Contents/Resources/app.asar.unpacked/dist/binaries/loom-mac-recorderproduction exited
[2019-02-04 14:29:37.555] [error] No menubar present
[2019-02-04 14:29:37.676] [error] No menubar present
[2019-02-04 14:29:39.537] [error] No menubar present
[2019-02-04 14:29:39.675] [error] No menubar present
[2019-02-04 14:29:41.653] [error] No menubar present
[2019-02-04 14:29:41.790] [error] No menubar present
Run Code Online (Sandbox Code Playgroud)

正如您从日志中看到的,所有退出事件都以正确的顺序触发,但应用程序仍保留在 mac 菜单栏中。发生的一件有趣的事情是[error] No menubar present. 每当我尝试单击菜单栏中的图标时,就会出现这种情况。当它试图隐藏应用程序菜单时它应该抛出。这是有问题的菜单栏:https : //github.com/maxogden/menubar

这是成功退出日志的样子

019-02-04 14:52:36.722] [info] Attempting to quit app
[2019-02-04 14:52:36.740] [info] stopping mouse tracking
[2019-02-04 14:52:36.868] [info] App will quit
[2019-02-04 14:52:36.874] [info] App is quitting
[2019-02-04 14:52:36.887] [info]  mac binary: main.swift:49 : sendPayload(messageToSend:): {"type":"will-close-expectedly"}

[2019-02-04 14:52:36.889] [info] daemon binary /Applications/LoomStaging.app/Contents/Resources/app.asar.unpacked/dist/binaries/loom-mac-recorderstaging exited
Run Code Online (Sandbox Code Playgroud)

有什么方法可以深入了解导致应用程序挂起的原因,因为在主进程中似乎没有抛出任何错误,只是出现了所有 UI 元素都消失但进程仍然存在的幽灵状态。有没有办法更深入地了解正在发生的事情/挂断的地方。

另一个有趣的点是,每当我们在更新时执行 quitAndInstall(使用电子构建器),如果前面提到的菜单栏是hidden,那么我们几乎总是会遇到这种奇怪的挂起状态,但是当菜单栏可见并且我们启动了 quitAndInstall 时,它工作正常。

san*_*ier 0

我已经用准系统示例创建了测试存储库

process.platform在我的机器上它工作正常(我删除了对事件的检查window-all-closed,但我在使用和不使用这些检查的情况下进行了测试 - 两种方式都有效):

const { menubar } = require('menubar');
const { app, Menu, Tray, autoUpdater, dialog } = require('electron');

autoUpdater.setFeedURL(`https://update.electronjs.org/sanperrier/menubar-test/${process.platform}-${process.arch}/${app.getVersion()}`);

autoUpdater.on('update-not-available', () => {
    dialog.showMessageBox({
        title: "No available updates",
        message: `No available updates\ncurrent version: ${app.getVersion()}`
    });
});

autoUpdater.on('update-available', () => {
    dialog.showMessageBox({
        title: "Update is downloading",
        message: `Update is downloading\ncurrent version: ${app.getVersion()}`
    });
});

autoUpdater.on('update-downloaded', async (e, notes, name) => {
    await dialog.showMessageBox({
        title: "Update downloaded",
        message: `Update downloaded\ncurrent version: ${app.getVersion()}\nnew version: ${name}`,
        detail: notes
    });

    autoUpdater.quitAndInstall();
});

app.on('ready', () => {
    const tray = new Tray(require.resolve('menubar/assets/IconTemplate.png'));
    const contextMenu = Menu.buildFromTemplate([
        {
            label: `Check for updates and install (current: ${app.getVersion()})`,
            type: 'normal',
            click: () => autoUpdater.checkForUpdates()
        },
        {
            label: 'Close window',
            type: 'normal',
            click: () => mb.window?.close()
        },
        {
            label: 'Exit',
            type: 'normal',
            click: () => app.quit()
        }
    ]);
    tray.setContextMenu(contextMenu);

    const mb = menubar({ tray });
});

app.on('window-all-closed', e => {
    console.log('window-all-closed');

    // if (process.platform != 'darwin') {
        app.quit();
    // }
});

app.on('before-quit', () => console.log('before-quit'));
app.on('will-quit', () => console.log('will-quit'));
app.on('quit', async () => console.log('quit'));

Run Code Online (Sandbox Code Playgroud)

工作正常。

可能的问题:

  1. 旧版本的电子
  2. 也许致电app.quit()平台window-all-closeddarwin有所帮助
  3. 我在源中搜索了“没有菜单栏” menubar,但没有找到任何内容,因此此错误源于您的代码或第 3 方代码。或者也许是旧版本menubar

请澄清一下,“菜单栏是”是什么意思hidden

作为解决方法,您可以尝试添加app.exit(0)quit事件处理程序。我不确定它会不会坏掉autoUpdate.quitAndInstall()。我用这样的代码测试了它:app.on('quit', () => { console.log('quit'); app.exit(0); })并且运行良好:

$ menubar-test.app/Contents/MacOS/menubar-test 
2020-08-08 13:35:20.689 menubar-test[62834:6522941] Download completed to: file:///Users/sanperrier/Library/Caches/com.electron.menubar-test.ShipIt/update.aaHFVTv/menubar-test-darwin-x64-1.1.11.zip
before-quit
will-quit
quit
Run Code Online (Sandbox Code Playgroud)

应用程序下载了更新并重新启动(安装了更新)。

希望这可以帮助。请随意分叉我的存储库并用它来确定问题的根本原因。