mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 15:41:17 +00:00
fix(logs): tolerate unavailable finalization IPC
Preserve window close and layout saves when session-log finalization IPC cannot be set up, and clear its timeout on setup failures. Follow-up for #4235 / PR #4236.
This commit is contained in:
1 parent
ee06726153
commit
7bad2f4939
2 files changed
+55
-3
No files matched your search
@@ -2,14 +2,21 @@ const { randomUUID } = require("node:crypto");
|
||||
|
||||
// Keep the renderer alive until its ConnectionLog replay writes have committed.
|
||||
function finalizeSessionLogs(webContents, { ipcMain, timeoutMs = 5000 } = {}) {
|
||||
if (!webContents || webContents.isDestroyed?.() || webContents.isCrashed?.()) {
|
||||
if (!webContents || webContents.isDestroyed?.() || webContents.isCrashed?.()
|
||||
|| typeof webContents.send !== "function"
|
||||
|| typeof ipcMain?.on !== "function"
|
||||
|| typeof ipcMain?.removeListener !== "function") {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const requestId = randomUUID();
|
||||
const finish = (success) => {
|
||||
clearTimeout(timer);
|
||||
ipcMain.removeListener("app:session-logs-finalized", onResult);
|
||||
try {
|
||||
ipcMain.removeListener("app:session-logs-finalized", onResult);
|
||||
} catch {
|
||||
// IPC teardown must not prevent the window from closing.
|
||||
}
|
||||
if (!success) console.warn("[SessionLogs] Renderer did not finalize logs before close");
|
||||
resolve(success);
|
||||
};
|
||||
@@ -19,8 +26,8 @@ function finalizeSessionLogs(webContents, { ipcMain, timeoutMs = 5000 } = {}) {
|
||||
}
|
||||
};
|
||||
const timer = setTimeout(() => finish(false), timeoutMs);
|
||||
ipcMain.on("app:session-logs-finalized", onResult);
|
||||
try {
|
||||
ipcMain.on("app:session-logs-finalized", onResult);
|
||||
webContents.send("app:finalize-session-logs", { requestId });
|
||||
} catch {
|
||||
finish(false);
|
||||
|
||||
@@ -83,6 +83,51 @@ test("unresponsive and destroyed renderers do not strand quit or leak listeners"
|
||||
assert.equal(ipcMain.listenerCount("app:session-logs-finalized"), 0);
|
||||
});
|
||||
|
||||
test("unavailable finalize IPC returns immediately without scheduling timers or sending", async (t) => {
|
||||
const timer = t.mock.method(globalThis, "setTimeout");
|
||||
const ipcMain = new EventEmitter();
|
||||
const send = t.mock.fn();
|
||||
const on = t.mock.method(ipcMain, "on");
|
||||
for (const unavailableIpc of [undefined, null, {}, { on() {} }, { removeListener() {} }, { on: true, removeListener() {} }, { on() {}, removeListener: true }]) {
|
||||
assert.equal(await finalizeSessionLogs({ send }, { ipcMain: unavailableIpc }), false);
|
||||
}
|
||||
for (const webContents of [undefined, null, {}, { send: true }]) {
|
||||
assert.equal(await finalizeSessionLogs(webContents, { ipcMain }), false);
|
||||
}
|
||||
assert.equal(timer.mock.callCount(), 0);
|
||||
assert.equal(on.mock.callCount(), 0);
|
||||
assert.equal(send.mock.callCount(), 0);
|
||||
});
|
||||
|
||||
for (const failure of ["registration", "send", "cleanup"]) {
|
||||
test(`finalize IPC ${failure} errors do not reject or leave a pending timeout`, async (t) => {
|
||||
t.mock.timers.enable({ apis: ["setTimeout"] });
|
||||
const warn = t.mock.method(console, "warn", () => {});
|
||||
const ipcMain = new EventEmitter();
|
||||
const webContents = { send() { throw new Error("send failed"); } };
|
||||
if (failure === "registration") {
|
||||
ipcMain.on = (channel, listener) => {
|
||||
EventEmitter.prototype.on.call(ipcMain, channel, listener);
|
||||
throw new Error("registration failed");
|
||||
};
|
||||
} else if (failure === "cleanup") {
|
||||
ipcMain.removeListener = (channel, listener) => {
|
||||
EventEmitter.prototype.removeListener.call(ipcMain, channel, listener);
|
||||
throw new Error("cleanup failed");
|
||||
};
|
||||
}
|
||||
let result;
|
||||
const pending = finalizeSessionLogs(webContents, { ipcMain }).then((success) => { result = success; });
|
||||
await tick();
|
||||
assert.equal(result, false, "failed setup must finish before the timeout");
|
||||
await pending;
|
||||
assert.equal(ipcMain.listenerCount("app:session-logs-finalized"), 0);
|
||||
assert.equal(warn.mock.callCount(), 1);
|
||||
t.mock.timers.tick(5000);
|
||||
assert.equal(warn.mock.callCount(), 1, "the cleared timeout must not run after finalization");
|
||||
});
|
||||
}
|
||||
|
||||
test("tray quit finalizes only after the dirty guard accepts and waits before quitting", async () => {
|
||||
const h = renderer();
|
||||
let finishCapture;
|
||||
|
||||
Reference in new issue
Block a user