Files
Netcatty/electron/bridges/sessionLogShutdown.cjs
T
陈大猫 51cb14955c fix(logs): finalize session logs on quit and pre-connect close (#4236)
* fix(logs): finalize session logs when the app quits without closing tabs

Fixes #4235

* fix(logs): finalize logs when unconnected sessions close

Capture never-connected terminal logs at explicit session removal, including empty buffers, without finalizing logs during runtime remounts. Cover tab close, failed-dialog close, batch close, and workspace close with persistence regressions.

Fixes #4235

* 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.
2026-11-14 21:36:08 +08:00

39 lines
1.3 KiB
JavaScript

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?.()
|| 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);
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);
};
const onResult = (event, payload) => {
if (event.sender === webContents && payload?.requestId === requestId) {
finish(payload.success === true);
}
};
const timer = setTimeout(() => finish(false), timeoutMs);
try {
ipcMain.on("app:session-logs-finalized", onResult);
webContents.send("app:finalize-session-logs", { requestId });
} catch {
finish(false);
}
});
}
module.exports = { finalizeSessionLogs };