Skip to content

Commit 6598983

Browse files
authored
docs(updater-relaunch): cut the context down to current behaviour and invariants (#1355) (#1396)
Trim updater-relaunch.md context (#1355).
1 parent 5706445 commit 6598983

1 file changed

Lines changed: 95 additions & 98 deletions

File tree

‎.ai/contexts/updater-relaunch.md‎

Lines changed: 95 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -8,108 +8,105 @@ covers:
88
# Linux update relaunch (#590)
99

1010
**Purpose**: after a Linux update installs, start the new version through the
11-
user's service manager instead of a child of the old process, and fall back to a
12-
visible manual-restart notice when that cannot be done.
11+
user's service manager, not as a child of the old process (which would inherit
12+
its NoNewPrivs), and fall back to a visible manual-restart notice when that
13+
fails. Code: `src/main/app/relaunch.js` (the helper), `src/main/app/linux-startup.js`
14+
(`acquireInstanceLock`, `readNoNewPrivs`), `main.js` (updater wiring, notice).
1315

14-
| File | Role |
15-
|---|---|
16-
| `src/main/app/relaunch.js` | The exported async `relaunch({ execPath, log, notify, quit, … })`: environment, `systemd-run` argv, timeouts, fallback. |
17-
| `src/main/app/linux-startup.js` | `acquireInstanceLock` (the marked relaunch's lock retry) and `readNoNewPrivs` (the startup diagnostic). |
18-
| `main.js` | `autoRunAppAfterInstall = false` on Linux, `appimage-filename-updated`, the explicit `install(false, false)` then `relaunch`, the `relaunch-required` notice. |
19-
| `test/relaunch.test.js` | The helper, the lock retry, the NoNewPrivs diagnostic and the installed electron-updater branches. |
16+
## The helper
2017

21-
`src/main/app/relaunch.js` exports one async function for callers, including the future async
22-
installer in #475. Call it after successful installation, passing `quit` and an
23-
async `notify(message)` that resolves after the user has seen and acknowledged
24-
the manual restart notice. It submits a transient **user service** with
25-
`systemd-run --user --unit=switchboard-relaunch-<timestamp>-<pid> --collect`.
26-
There is no `--scope`: the user manager starts the service, so it does not inherit
27-
the calling Electron process's NoNewPrivs flag. The manager itself must be
28-
unrestricted; this cannot repair a restricted user manager. The service inherits
29-
the manager's environment, with the caller's entire cleaned environment forwarded
30-
explicitly, including HOME, CLAUDE_CONFIG_DIR, SWITCHBOARD_TRIGGERS_DIR, PATH,
31-
SSH_AUTH_SOCK, locale and XDG settings. It reuses `src/main/shared/clean-env.js` (`cleanEnv` with `locale: false`) to exclude Claude
32-
session identities, Electron internals, Node injection options and the existing
33-
child-process exclusions. Additionally, CHROME_CRASHPAD_PIPE_NAME and CHROME_RESTART
34-
belong to the old Chromium process; INVOCATION_ID, JOURNAL_STREAM, SYSTEMD_EXEC_PID,
35-
NOTIFY_SOCKET and WATCHDOG_PID/USEC identify its old service; SWITCHBOARD_RELAUNCH
36-
is reserved for relaunch state. None are forwarded. The caller's object is not mutated.
37-
`ELECTRON_OZONE_PLATFORM_HINT` is the one Electron variable kept: it configures the app
38-
itself, not a child. Names systemd rejects (anything outside `[A-Za-z_][A-Za-z0-9_]*`,
39-
such as bash's exported `BASH_FUNC_x%%`) are skipped and logged, since one of them
40-
would make systemd-run fail and send every update to the manual fallback.
41-
Only names appear in `--setenv=NAME`; values are supplied in systemd-run's own
42-
environment, keeping credentials out of its command line. This name-only syntax
43-
was added in systemd 250: compare the [v249 parser](https://github.com/systemd/systemd/blob/v249/src/run/run.c)
44-
with the [v250 parser](https://github.com/systemd/systemd/blob/v250/src/run/run.c)
45-
and the [v250 manual](https://github.com/systemd/systemd/blob/v250/man/systemd-run.xml).
46-
Older systemd versions use the visible manual-restart fallback; no value-bearing
47-
argv or temporary environment file is used. Argv is passed without a shell;
48-
literal dollars are doubled to suppress systemd's own environment expansion.
49-
`--service-type=exec` makes target exec failures use the visible fallback as well.
50-
`--property=KillMode=process` preserves independently surviving daemon jobs, tmux
51-
servers and detached applications when the main process exits. With this mode,
52-
systemd targets only the main process for both termination and final killing;
53-
SendSIGKILL=no would unnecessarily disable escalation for a stuck main process.
54-
This deliberately accepts the child lifecycle tradeoff described in the
55-
[systemd.kill manual](https://www.freedesktop.org/software/systemd/man/latest/systemd.kill.html).
56-
Service submission has a five-second timeout. On ETIMEDOUT, a bounded two-second
57-
`systemctl --user is-active --quiet <same-unit>` check avoids showing the fallback
58-
if the service is already active. An inactive, missing or unqueryable unit keeps
59-
the fallback. There is a residual race if an activating unit becomes active after
60-
the check; fully cancelling/reconciling a queued job would broaden this helper's
61-
contract. Inspect the unit journal before starting another copy in that case.
62-
A missing executable, other spawn error, signal or nonzero exit also uses the
63-
quit-only fallback. Main sends the
64-
existing `updater-event` channel; the renderer shows an error-kind app notice,
65-
which has no expiry or dismiss button, with a sole Close Switchboard action
66-
(`dismissible: false`, `dismissOnAction: false`). Main waits for that action
67-
from its own renderer or for the window to close before quitting. No native
68-
dialog is used. Acceptance by systemd is not proof of a successful app startup;
69-
inspect the unit's journal if the new app does not appear. These service and
70-
argument semantics follow the [systemd-run manual](https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html).
18+
`relaunch({ execPath, log, notify, quit, … })` is for any caller (the async
19+
installer of #475 included), after a successful install; `notify(message)`
20+
resolves once the user acknowledged the notice. It runs, without a shell,
21+
`systemd-run --user --unit=switchboard-relaunch-<timestamp>-<pid> --collect
22+
--service-type=exec --property=KillMode=process --working-directory=<cwd>
23+
--setenv=NAME… -- <execPath> <argv> --switchboard-relaunch`.
7124

72-
In electron-updater 6.8.9, `BaseUpdater.quitAndInstall(false, false)` ignores the
73-
second argument and substitutes `autoRunAppAfterInstall`. `DebUpdater.doInstall`
74-
runs its package manager synchronously, then calls its Electron adapter's
75-
`app.relaunch()` when isForceRunAfter is true. There is no public post-install
76-
success event. Linux's explicit install therefore calls the installed updater's
77-
`install(false, false)` method and checks its boolean result, then awaits the
78-
helper. This is a deliberately narrow dependency on BaseUpdater's internal API;
79-
package.json pins 6.8.9 exactly (the lock already resolves that version). Tests
80-
execute both the installed BaseUpdater and DebUpdater force-run branch, stubbing
81-
the package-manager boundary and proving false does not call app.relaunch.
82-
A false result
83-
resets its quitAndInstallCalled latch like BaseUpdater.quitAndInstall does.
84-
The helper's quit callback sets updateInstalling and enters the existing shared
85-
shutdown drain. Throws and updater error events reset the failure state.
25+
- **A service, never `--scope`**: the user manager starts it, so it does not
26+
inherit NoNewPrivs. A restricted user manager cannot be repaired from here.
27+
- **Environment**: on top of the manager's, the caller's whole environment, so
28+
HOME, CLAUDE_CONFIG_DIR, SWITCHBOARD_TRIGGERS_DIR, PATH, SSH_AUTH_SOCK, locale
29+
and XDG settings follow it, through `cleanEnv(env, { locale: false })`
30+
(session identities, `ELECTRON_*`, `NODE_OPTIONS`, the other child
31+
exclusions), plus `ELECTRON_OZONE_PLATFORM_HINT`, kept because it configures
32+
the app itself. Never forwarded: `CHROME_CRASHPAD_PIPE_NAME`, `CHROME_RESTART`
33+
(old Chromium), `INVOCATION_ID`, `JOURNAL_STREAM`, `SYSTEMD_EXEC_PID`,
34+
`NOTIFY_SOCKET`, `WATCHDOG_PID`, `WATCHDOG_USEC` (old service), `SWITCHBOARD_RELAUNCH`
35+
(reserved). The caller's object is not mutated.
36+
- Names outside `[A-Za-z_][A-Za-z0-9_]*` (`BASH_FUNC_x%%`) are skipped and
37+
logged: one would fail systemd-run and send every update to the fallback.
38+
- **Environment values never in argv**, keeping credentials off the command
39+
line: `--setenv=NAME` only, values in systemd-run's own environment. This
40+
needs systemd 250 ([v249](https://github.com/systemd/systemd/blob/v249/src/run/run.c)
41+
vs [v250](https://github.com/systemd/systemd/blob/v250/src/run/run.c) parser,
42+
[v250 manual](https://github.com/systemd/systemd/blob/v250/man/systemd-run.xml));
43+
older systemd takes the fallback, never a value-bearing argv or env file.
44+
- Dollars in the forwarded arguments are doubled against systemd's expansion;
45+
an inherited `--switchboard-relaunch` is stripped before the marker is added.
46+
- `--service-type=exec` sends an exec failure of the target to the fallback.
47+
- `KillMode=process` keeps daemon jobs, tmux servers and detached apps alive
48+
when the main process exits: systemd terminates and finally kills only the
49+
main process, still escalating for a stuck one (`SendSIGKILL=no` would not). The child lifecycle tradeoff is
50+
deliberate ([systemd.kill](https://www.freedesktop.org/software/systemd/man/latest/systemd.kill.html)).
51+
- Submission times out at 5 s. On `ETIMEDOUT`, a 2 s `systemctl --user
52+
is-active --quiet <same unit>` skips the fallback if the unit is active;
53+
inactive, missing or unqueryable keeps it.
54+
Residual race: a unit active only after the check; reconciling the queued
55+
job would broaden the contract, so inspect its journal before starting a copy.
56+
- **Fallback**: a spawn error (missing executable included; thrown or returned,
57+
bar that active unit), a signal or a nonzero exit awaits `notify`, then quits
58+
without relaunching.
59+
Systemd's acceptance does not prove the app started: read the unit's journal.
60+
See the [systemd-run manual](https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html).
8661

87-
A native macOS install that arrives after the 30-second release bound can show
88-
the ordinary close wording rather than Restart wording; cancelling that close
89-
clears the install bypass so later Cmd+Q checks edits and sessions again.
90-
autoRunAppAfterInstall is also disabled on Linux. Other platforms retain the
91-
original zero-argument quitAndInstall call.
62+
**The notice**: `relaunch-required` on `updater-event` becomes the error-kind
63+
app notice `updater-relaunch` — no expiry, no dismiss button, a sole **Close
64+
Switchboard** action (`dismissible: false`, `dismissOnAction: false`). Main
65+
waits for that action from its own renderer or for the window to close; with no
66+
live window it logs the message and goes on. No native dialog.
9267

93-
Install-on-quit retains the autoUpdate setting: BaseUpdater.addQuitHandler uses
94-
`install(true, false)` already. It installs without restarting, as before. No
95-
extra relaunch is added when a user deliberately quits. AppImage's renamed
96-
destination is remembered through appimage-filename-updated; relaunch executes
97-
that AppImage instead of the old mounted executable. AppImage installation
98-
still runs its own exit-after-install integration subprocess, without starting
99-
an interactive instance. Pacman and RPM use the same no-force-run contract.
68+
## electron-updater wiring
10069

101-
The new process carries `--switchboard-relaunch`. Only on Linux with that marker
102-
does single-instance-lock acquisition (`acquireInstanceLock`) retry every 200 ms
103-
for at most ten seconds.
104-
The old instance finishes its normal bounded shutdown while the replacement
105-
waits. Ordinary second launches still quit and focus the existing window.
106-
Once updateInstalling is set, the old instance ignores second-instance events
107-
instead of repeatedly restoring and focusing its window. Exhausting a marked
108-
launch's retry bound logs a warning through the main logger before quitting.
109-
The startup diagnostic (`readNoNewPrivs`) reads /proc/self/status on Linux and logs NoNewPrivs as
110-
0, 1 or unknown. Only 1 produces an error-kind app notice, sent on did-finish-load
111-
and again on renderer reload. NoNewPrivs cannot be cleared in the current process.
70+
electron-updater is pinned to exactly 6.8.9 in `package.json` (the lock
71+
resolves it), because Linux
72+
relies on BaseUpdater's internal `install`:
11273

113-
Qualification here uses injected process, spawn, clock and procfs boundaries on
114-
Windows. It does not prove the NoNewPrivs value of a real Linux service or its
115-
desktop environment. Run the before/after commands in the #590 report on Linux.
74+
- `quitAndInstall(false, false)` ignores its second argument and uses
75+
`autoRunAppAfterInstall`; and `DebUpdater.doInstall` runs its package manager
76+
synchronously, then `app.relaunch()` when `isForceRunAfter` (Pacman and RPM
77+
likewise). There is no public post-install success event.
78+
- So Linux sets `autoRunAppAfterInstall = false`, calls `install(false, false)`,
79+
checks the boolean, then awaits the helper. False runs `resetUpdateAttempt`,
80+
which resets `quitAndInstallCalled` as `quitAndInstall` does; throws and
81+
updater `error` events reset the failure state too. The helper's `quit` sets
82+
`updateInstalling` and enters the shared shutdown drain.
83+
- Other platforms keep the zero-argument `quitAndInstall()`. A native macOS
84+
install arriving after the 30-second release bound can show the ordinary
85+
close wording instead of Restart; cancelling that close clears the install
86+
bypass, so a later Cmd+Q checks edits and sessions again.
87+
- Install-on-quit follows the autoUpdate setting: `addQuitHandler` calls
88+
`install(true, false)`, so no restart and no relaunch on a deliberate quit.
89+
- AppImage: the helper executes the renamed file from
90+
`appimage-filename-updated`, not the old mounted executable; the install
91+
still runs its exit-after-install integration subprocess, with no
92+
interactive instance.
93+
94+
## The new instance
95+
96+
- Only on Linux with `--switchboard-relaunch` does `acquireInstanceLock` retry,
97+
every 200 ms for at most 10 s, while the old instance finishes its bounded
98+
shutdown; exhaustion logs a warning through the main logger, then quits.
99+
Ordinary second launches still quit and focus the existing window.
100+
- Once `updateInstalling` is set, the old instance ignores `second-instance`
101+
instead of restoring and focusing its window again and again.
102+
- `readNoNewPrivs` logs NoNewPrivs from `/proc/self/status` on Linux as 0, 1 or
103+
unknown. Only 1 produces an error-kind notice, sent on `did-finish-load` and
104+
again on renderer reload: the flag cannot be cleared in a running process.
105+
106+
## Tests
107+
108+
`test/relaunch.test.js` injects process, spawn, clock and procfs, and executes
109+
the installed BaseUpdater and DebUpdater with the package manager stubbed,
110+
proving false does not call `app.relaunch`. It runs on Windows and proves
111+
neither a real Linux service's NoNewPrivs nor its desktop: run the #590
112+
report's before/after commands on Linux.

0 commit comments

Comments
 (0)