diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index 257c8ac575d9..306388f340d3 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -146,7 +146,7 @@ const throwRPCError = function (message) { throw error } -const rendererMissingErrorMessage = (meta, args, callIntoRenderer) => { +const removeRemoteListenersAndLogWarning = (meta, args, callIntoRenderer) => { let message = `Attempting to call a function in a renderer window that has been closed or released.` + `\nFunction provided here: ${meta.location}` @@ -218,7 +218,7 @@ const unwrapArgs = function (sender, args) { if (!sender.isDestroyed() && webContentsId === sender.getId()) { sender.send('ELECTRON_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args)) } else { - console.warn(rendererMissingErrorMessage(meta, args, callIntoRenderer)) + console.warn(removeRemoteListenersAndLogWarning(meta, args, callIntoRenderer)) } } diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index 9f4ce0fb1ebc..32cc25041c52 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -495,7 +495,7 @@ describe('ipc module', function () { assert.equal(w.listenerCount('test'), 0) }) - it('shows a warning when a function is called in a destroyed renderer', (done) => { + it('detaches listeners subscribed to destroyed renderers, and shows a warning', (done) => { w = new BrowserWindow({ show: false }) @@ -506,8 +506,10 @@ describe('ipc module', function () { 'Function provided here: remote-event-handler.html:11:33', 'Remote event names: remote-handler, other-remote-handler' ].join('\n') - const warningMessage = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler') + const {warningMessage, listenerCountBefore, listenerCountAfter} = + ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler') assert.equal(warningMessage, expectedMessage) + assert.equal(listenerCountAfter, listenerCountBefore - 1) done() }) w.webContents.reload() diff --git a/spec/static/main.js b/spec/static/main.js index a0ce1c24d380..50fe5352eb5f 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -252,12 +252,20 @@ ipcMain.on('prevent-next-new-window', (event, id) => { ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => { const consoleWarn = console.warn - let lastWarning = null + let warningMessage = null console.warn = (message) => { - lastWarning = message + warningMessage = message } + const contents = webContents.fromId(id) + const listenerCountBefore = contents.listenerCount(eventName) contents.emit(eventName, {sender: contents}) - event.returnValue = lastWarning + const listenerCountAfter = contents.listenerCount(eventName) + + event.returnValue = { + warningMessage, + listenerCountBefore, + listenerCountAfter + } console.warn = consoleWarn })