mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* fix(auto-update): commit app to quit before quitAndInstall on macOS (#1215) macOS in-place auto-update downloaded and unpacked the new version but never installed it: the app appeared to close, ShipIt never ran, and no restart happened. A full uninstall + reinstall did not help; only a manual DMG replace worked. Root cause is a code-level coordination bug, not the release pipeline. The published mac zips are correctly Developer-ID signed, notarized, and stapled (Team H7WS5L2ML4, consistent across 1.1.17 and 1.1.20), and latest-mac.yml is well-formed — so Squirrel.Mac signature validation passes. The failure is that quitAndInstall() drives app.quit() while two normal-quit behaviors keep the process alive: 1. the main-window close handler hides to tray when close-to-tray is enabled (it only closes when isQuitting is true), and 2. the before-quit dirty-editor guard preventDefault()s the quit for a 5s renderer round-trip. Either keeps the parent process running, so Squirrel.Mac's ShipIt helper — which waits on the parent PID to die before swapping the bundle — lands in launchd "pending spawn / on-demand-only" limbo and the service is removed without installing. This matches the reporter's diagnosis exactly ("ShipIt 没有真正启动安装器", launchd on-demand-only). Fix: before quitAndInstall fires app.quit(), mark the app as quitting for an update via windowManager.setQuittingForUpdate(true). That sets isQuitting (bypassing close-to-tray) and the before-quit handler now returns early when isQuittingForUpdate() is true (skipping the dirty-editor round-trip), so the process exits cleanly and ShipIt can run. The same fix also covers the latent Windows NSIS case where close-to-tray would block an in-place update. If the install never actually quits the app (quitAndInstall throws, or returns without app.quit() on a Squirrel follow-up error / stale download), the quitting-for-update flags are rolled back — synchronously on throw, and via a short unref'd watchdog otherwise — so the app does not get stuck permanently bypassing close-to-tray and the quit guard. Tests: unit tests for the new windowManager flags and for the install handler — ordering (setQuittingForUpdate before quitAndInstall), tray cleanup still runs, no-op when the updater fails to load, rollback on synchronous throw, and watchdog rollback when the app never quits. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(auto-update): keep dirty-editor guard during update install setQuittingForUpdate only bypasses close-to-tray (so the window actually closes and Squirrel.Mac's ShipIt can swap the bundle); it must NOT skip the unsaved-work guard, or clicking "Restart Now" with a dirty SFTP editor would silently lose edits. If the user cancels to save, the quit aborts and autoUpdateBridge's watchdog clears the quitting-for-update flags. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(auto-update): clear update-quit state when the quit is cancelled When the user clicks "Restart Now" with a dirty editor open, the before-quit guard cancels the quit (settle "stay"). The update path had already called setQuittingForUpdate(true) (which flips isQuitting=true to bypass close-to-tray for the install), so without clearing it the app stays in a quitting state — close-to-tray and other !isQuitting-gated behavior bypassed — until the 10s watchdog fires. Clear it immediately on the cancelled-quit path (#1215 review). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(auto-update): check for unsaved editors before quitAndInstall (#1215) The previous fix bypassed close-to-tray so the app process actually exits and Squirrel.Mac's ShipIt can swap the bundle, while keeping the before-quit dirty-editor guard as the unsaved-work safety net. But on macOS that net has a hole: quitAndInstall() closes the window FIRST and only then fires before-quit. Once setQuittingForUpdate(true) lets the main window truly close (instead of hiding to tray), the before-quit guard can run after the window is already gone — isReachableByUser is false, so it commits the quit and silently drops unsaved SFTP edits. Fix: move the dirty-editor check to the moment the user clicks "Restart Now", in the install handler, BEFORE setQuittingForUpdate / quitAndInstall — while the window and renderer are still alive: - dirty -> abort the install (don't set the quitting flags, don't quitAndInstall) and broadcast netcatty:update:needs-save so the renderer prompts the user to save and retry. - clean -> proceed with the existing flow (commit-to-quit, tray cleanup, quitAndInstall, watchdog). - no reachable main window / crashed renderer -> install directly (no user to ask), matching the before-quit fail-open path. The before-quit dirty guard is kept as defense-in-depth: if the window is still reachable it re-checks (clean, since we just verified), and if it's already gone it lets the quit through — which is now safe because the install handler already confirmed there were no unsaved editors. The request/reply/timeout round-trip is extracted into a shared helper, electron/bridges/dirtyEditorGuard.cjs (queryDirtyEditors), so the install handler and main.cjs's before-quit guard use one implementation. main.cjs's before-quit is refactored onto it (behavior preserved: sender-filtered reply, fail-open timeout, and the setQuittingForUpdate(false) rollback when the user cancels to save). The needs-save notice is BROADCAST to every window, not just the queried main window: "Restart to Update" can be clicked from the Settings window, which would otherwise see the click do nothing. preload exposes onUpdateNeedsSave; the subscription lives in useUpdateCheck (state layer), and both consumers — App.tsx (main window) and SettingsPage (settings window) — pass an onNeedsSave callback that shows an actionable toast ("save your editors, then click Restart Now again") in en / zh-CN / ru. Also lengthen the quitting-for-update rollback watchdog from 10s to 60s. On macOS quitAndInstall() can return while Squirrel is still pulling the downloaded ZIP from the local update server before it closes the windows; on a large/slow update that can exceed 10s. Clearing isQuitting that early would let the eventual native quit hit a non-quitting close-to-tray handler and strand the install again — the exact #1215 failure. The longer window only fires when the app is realistically stuck, at the cost of close-to-tray staying bypassed a little longer in the rare genuine-failure case. Tests: queryDirtyEditors (result / no-dirty / timeout / wrong-sender / dead or crashed webContents / send-throws / no-ipcMain paths); install handler pre-check (dirty -> no quitAndInstall + needs-save broadcast to all windows; clean -> quitAndInstall runs; no main window -> installs without asking). Existing install-handler tests updated for the now-async handler. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
100 lines
3.9 KiB
JavaScript
100 lines
3.9 KiB
JavaScript
/**
|
|
* Dirty-editor guard helper.
|
|
*
|
|
* Both the before-quit handler (electron/main.cjs) and the auto-update install
|
|
* handler (electron/bridges/autoUpdateBridge.cjs) need to ask the renderer
|
|
* whether any SFTP editor tab has unsaved changes before letting the process
|
|
* exit. This module centralizes that one-shot request/response round-trip so
|
|
* the two call sites stay in sync (#1215).
|
|
*
|
|
* The renderer side lives in application/app/useAppStartupEffects.ts: it
|
|
* listens for "app:query-dirty-editors" and replies on
|
|
* "app:dirty-editors-result" with { hasDirty: boolean }.
|
|
*/
|
|
|
|
/**
|
|
* Ask a specific renderer whether it has unsaved editor changes.
|
|
*
|
|
* Sends "app:query-dirty-editors" to the given webContents and resolves with
|
|
* the renderer's reply. Resolves `false` (fail-open) if the renderer never
|
|
* answers within `timeoutMs`, if the webContents can't be messaged, or if the
|
|
* send throws — in every one of those cases there is no usable UI to surface a
|
|
* "save first" warning on, so blocking the quit would only strand the user.
|
|
*
|
|
* Only a reply whose `event.sender` is the exact `webContents` we queried is
|
|
* accepted; replies from any other window are ignored so a stray/rogue message
|
|
* can't decide the result. The listener and timer are always torn down before
|
|
* resolving, so a late timeout can't override an already-received reply (and
|
|
* vice versa).
|
|
*
|
|
* @param {import("electron").WebContents | null | undefined} webContents
|
|
* @param {number} timeoutMs - Max time to wait for the renderer reply.
|
|
* @param {{ ipcMain?: import("electron").IpcMain }} [options] - Inject ipcMain
|
|
* for tests; defaults to electron's ipcMain.
|
|
* @returns {Promise<boolean>} true when the renderer reports unsaved changes.
|
|
*/
|
|
function queryDirtyEditors(webContents, timeoutMs, options = {}) {
|
|
const ipcMain = options.ipcMain || resolveIpcMain();
|
|
|
|
// No renderer to ask, or no ipcMain to listen with — fail open.
|
|
if (!ipcMain || !webContents) return Promise.resolve(false);
|
|
if (webContents.isDestroyed?.() || webContents.isCrashed?.()) {
|
|
return Promise.resolve(false);
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
let settled = false;
|
|
let timeoutId = null;
|
|
|
|
const settle = (hasDirty) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
if (timeoutId !== null) {
|
|
clearTimeout(timeoutId);
|
|
timeoutId = null;
|
|
}
|
|
ipcMain.removeListener("app:dirty-editors-result", onResult);
|
|
resolve(hasDirty);
|
|
};
|
|
|
|
function onResult(evt, payload) {
|
|
// Defence in depth: only the renderer we queried may decide the result.
|
|
// Use `.on` (not `.once`) so a rogue reply from another window doesn't
|
|
// consume the listener slot and let the real reply fall through. A
|
|
// missing/falsy sender is anomalous and treated as a wrong-window reply.
|
|
if (evt?.sender !== webContents) return;
|
|
settle(payload?.hasDirty === true);
|
|
}
|
|
ipcMain.on("app:dirty-editors-result", onResult);
|
|
|
|
// Timeout fallback: if the renderer never replies (crash, unhandled
|
|
// exception in its listener, etc.) we must not hang forever. Fail open.
|
|
timeoutId = setTimeout(() => settle(false), timeoutMs);
|
|
|
|
try {
|
|
webContents.send("app:query-dirty-editors");
|
|
} catch (err) {
|
|
// webContents.send can throw if the renderer was destroyed between the
|
|
// isCrashed?.() check above and this call (a real race when the GPU
|
|
// process is dying). Tear down synchronously and fail open.
|
|
console.warn("[DirtyEditorGuard] Failed to query renderer for dirty editors:", err);
|
|
settle(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Lazily resolve electron's ipcMain. Kept out of module top-level so this file
|
|
* can be required in a plain `node --test` process (where `electron` isn't a
|
|
* loadable module) as long as the caller injects ipcMain.
|
|
*/
|
|
function resolveIpcMain() {
|
|
try {
|
|
return require("electron").ipcMain;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
module.exports = { queryDirtyEditors };
|