fix(plugins): skip destroyed WebContents during scope broadcasts

This commit is contained in:
陈奇 committed 2026-10-27 12:04:34 +00:00
1 parent cbe89995e6
commit a56d1ab785
2 files changed
+30 -1

No files matched your search

@@ -0,0 +1,29 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
test("actual plugin broadcast skips destroyed WebContents while its BrowserWindow is still alive", () => {
// Evaluate the actual registrar callback with narrow window fixtures. This
// models the destruction-event ordering, not an installed Electron crash.
const source = fs.readFileSync(path.join(__dirname, "../main/registerBridges.cjs"), "utf8");
const callbacks = [...source.matchAll(/broadcast: \(channel, payload\) => \{([\s\S]*?)\n \},/g)];
assert.equal(callbacks.length, 1);
const received = [];
const deadWindow = { isDestroyed: () => true, get webContents() { throw Error("Destroyed BrowserWindow getter"); } };
const closingWindow = { isDestroyed: () => false, webContents: {
isDestroyed: () => true, send() { throw Error("Object has been destroyed"); },
} };
const liveWindow = { isDestroyed: () => false, webContents: {
isDestroyed: () => false, send: (...args) => received.push(args),
} };
const broadcast = vm.runInNewContext(`(channel, payload) => {${callbacks[0][1]}\n}`, {
electronModule: { BrowserWindow: { getAllWindows: () => [deadWindow, closingWindow, liveWindow] } },
});
const payload = { host: [], session: [] };
assert.doesNotThrow(() => broadcast("netcatty:plugins:scope-catalog-changed", payload));
assert.deepEqual(received, [["netcatty:plugins:scope-catalog-changed", payload]]);
});
+1 -1
View File
@@ -240,7 +240,7 @@ function createBridgeRegistrar(context) {
isTrustedSender: createTrustedPluginBridgeSender({ devServerUrl: effectiveDevServerUrl }),
broadcast: (channel, payload) => {
for (const window of electronModule.BrowserWindow.getAllWindows()) {
if (!window.isDestroyed()) window.webContents.send(channel, payload);
if (!window.isDestroyed() && !window.webContents.isDestroyed()) window.webContents.send(channel, payload);
}
},
});