From 170f587f28ac5ce24a467093d928bb6a7be8bcb7 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 11 Apr 2021 11:58:45 +0200 Subject: [PATCH] Desktop: Fixes #4570: Prevents plugin from causing an error when the app closes --- packages/app-desktop/ElectronAppWrapper.ts | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/app-desktop/ElectronAppWrapper.ts b/packages/app-desktop/ElectronAppWrapper.ts index 01bc505aa2..cbb26217b5 100644 --- a/packages/app-desktop/ElectronAppWrapper.ts +++ b/packages/app-desktop/ElectronAppWrapper.ts @@ -189,18 +189,27 @@ export default class ElectronAppWrapper { // This handler receives IPC messages from a plugin or from the main window, // and forwards it to the main window or the plugin window. ipcMain.on('pluginMessage', (_event: any, message: PluginMessage) => { - if (message.target === 'mainWindow') { - this.win_.webContents.send('pluginMessage', message); - } - - if (message.target === 'plugin') { - const win = this.pluginWindows_[message.pluginId]; - if (!win) { - this.logger().error(`Trying to send IPC message to non-existing plugin window: ${message.pluginId}`); - return; + try { + if (message.target === 'mainWindow') { + this.win_.webContents.send('pluginMessage', message); } - win.webContents.send('pluginMessage', message); + if (message.target === 'plugin') { + const win = this.pluginWindows_[message.pluginId]; + if (!win) { + this.logger().error(`Trying to send IPC message to non-existing plugin window: ${message.pluginId}`); + return; + } + + win.webContents.send('pluginMessage', message); + } + } catch (error) { + // An error might happen when the app is closing and a plugin + // sends a message. In which case, the above code would try to + // access a destroyed webview. + // https://github.com/laurent22/joplin/issues/4570 + console.error('Could not process plugin message:', message); + console.error(error); } });