|
| 1 | +import * as Duration from "effect/Duration"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | + |
| 4 | +import * as ElectronApp from "../../electron/ElectronApp.ts"; |
| 5 | +import * as DesktopShutdown from "../../app/DesktopShutdown.ts"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Restarting into a freshly installed build. |
| 9 | + * |
| 10 | + * This deliberately does **not** call `DesktopLifecycle.relaunch`, even though that exists and does |
| 11 | + * almost this. Two differences make it unusable here, and both are load-bearing: |
| 12 | + * |
| 13 | + * 1. **Its shutdown wait is unbounded.** `requestDesktopShutdownAndWait` awaits a bare `Deferred` |
| 14 | + * that resolves only once every backend has stopped, and `DesktopBackendManager.closeRun` takes |
| 15 | + * a no-timeout branch when given no options. A wedged PTY drain, an SSH tunnel, or a `wsl.exe` |
| 16 | + * backend that never returns means the relaunch never fires — the app sits on "Restarting…" |
| 17 | + * indefinitely. That is issue #41 reached through a different door: an app that is dark, for an |
| 18 | + * unbounded time, with no error anywhere. |
| 19 | + * |
| 20 | + * 2. **It re-execs `process.execPath`.** Under macOS App Translocation that points into a |
| 21 | + * randomised read-only snapshot, so the app would come back on the OLD build while |
| 22 | + * post-install verification passed against the wrong process. `installTarget.ts` refuses |
| 23 | + * translocated paths before we ever get here, but the exec path still has to come from the |
| 24 | + * install target rather than from the running process. |
| 25 | + * |
| 26 | + * Editing `DesktopLifecycle.ts` to add a timeout was the other option. It would have added seam |
| 27 | + * row 36 to a ledger whose own tripwire asks for re-isolation first, on a file the fork does not |
| 28 | + * currently touch — so this restart path lives here instead. |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * How long to let a graceful shutdown run before forcing the exit. |
| 33 | + * |
| 34 | + * Long enough for a healthy teardown — backends stop in well under a second normally — and short |
| 35 | + * enough that a hung one costs the user ten seconds rather than their whole session. The relaunch |
| 36 | + * is armed before the timer starts, so a forced exit still comes back up. |
| 37 | + */ |
| 38 | +export const SHUTDOWN_GRACE = Duration.seconds(10); |
| 39 | + |
| 40 | +export type RestartOutcome = "graceful" | "forced-after-timeout"; |
| 41 | + |
| 42 | +/** |
| 43 | + * Quit and come back on the new build. |
| 44 | + * |
| 45 | + * Ordering is the point of this function: |
| 46 | + * |
| 47 | + * 1. Ask for shutdown and wait, bounded. |
| 48 | + * 2. Arm the relaunch — immediately before exiting, never earlier. `app.relaunch()` registers |
| 49 | + * intent for the *next* quit, so arming it before a wait that might fail would leave the app |
| 50 | + * alive with a booby-trapped Cmd-Q that silently resurrects it later. |
| 51 | + * 3. Exit, whether or not the shutdown finished. |
| 52 | + */ |
| 53 | +export const restartIntoInstalledBuild = Effect.fn("t3x.updateDelivery.restart")(function* (args: { |
| 54 | + readonly execPath: string; |
| 55 | + readonly argv: readonly string[]; |
| 56 | +}) { |
| 57 | + const electronApp = yield* ElectronApp.ElectronApp; |
| 58 | + const shutdown = yield* DesktopShutdown.DesktopShutdown; |
| 59 | + |
| 60 | + yield* shutdown.request; |
| 61 | + |
| 62 | + // `Effect.timeout` fails on expiry; the failure is the signal to stop waiting, not an error to |
| 63 | + // report. Either way we exit — the only difference is whether backends got to close cleanly. |
| 64 | + const outcome: RestartOutcome = yield* shutdown.awaitComplete.pipe( |
| 65 | + Effect.timeout(SHUTDOWN_GRACE), |
| 66 | + Effect.as<RestartOutcome>("graceful"), |
| 67 | + Effect.catch(() => Effect.succeed<RestartOutcome>("forced-after-timeout")), |
| 68 | + ); |
| 69 | + |
| 70 | + if (outcome === "forced-after-timeout") { |
| 71 | + yield* Effect.logWarning( |
| 72 | + "t3x: shutdown did not finish within the grace period; forcing exit so the relaunch still happens", |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + yield* electronApp.relaunch({ execPath: args.execPath, args: [...args.argv] }); |
| 77 | + yield* electronApp.exit(0); |
| 78 | + |
| 79 | + return outcome; |
| 80 | +}); |
| 81 | + |
| 82 | +/** |
| 83 | + * The executable to re-exec, derived from the bundle we just installed into. |
| 84 | + * |
| 85 | + * Never `process.execPath` — see the translocation note above. |
| 86 | + */ |
| 87 | +export function executablePathForBundle(appBundlePath: string, appName: string): string { |
| 88 | + const binaryName = appName.endsWith(".app") ? appName.slice(0, -".app".length) : appName; |
| 89 | + return `${appBundlePath}/Contents/MacOS/${binaryName}`; |
| 90 | +} |
0 commit comments