fix(electron): report why a launch failed, and launch past macOS's reopen-windows alert

KD
4h ago
+179-39

What

When an Electron app fails to start, app.open() now says why, and a later launch does not stall:

  • A failed launch reports the app's own failure (the app quit before it opened a window ..., no window loaded a page ...) with its stderr, not the test's TEST_TIMEOUT.
  • A launch whose window loaded just as the wait ran out still gets its main process connected and its downloads folder set.
  • On macOS the app starts with -ApplePersistenceIgnoreState YES. An app the engine killed in an earlier test no longer stalls its next launch behind the system's "reopen windows?" alert.
  • A stderr line over 500 characters shows only its length in the message, so no part of a secret is quoted.
  • An attempt ends cleanly even when its user-data directory cannot be removed, and a dialog handler still running from one attempt never answers the next attempt's dialogs.

Why

  • The launch used the operation's whole time to wait for a window. When nothing loaded, the operation's own timeout fired first, so the test saw TEST_TIMEOUT and never the reason the engine had worked out.
  • On macOS a killed Electron app leaves saved window state. The next launch of that binary showed a modal alert before ready and waited its full timeout. This is what stalled the local testbed after an interrupted run.
  • Quoting a long stderr line in full would fill the report. A cut at any point, white space included, could keep the first part of a secret the app printed, and the run's redaction matches only whole values (AGENTS.md, security invariants).

How it works

  1. launch() keeps FAILURE_REPORT_MS (750 ms, at most half the time) out of the window wait. With under 1.5 s left, half is less than the report can take, so a very short launch can still end in TEST_TIMEOUT. On failure it asks the main process whether Electron is ready (250 ms each), kills the app, and waits 250 ms for it to exit (packages/electron/src/app-process.ts:183, :227). The next quit(), which runs before the user-data directory is removed, waits for the rest of that exit (app-process.ts:159-169).
  2. Setting up the app after its window loaded uses whatever time is left, at least 1 ms, rather than failing when none is left (app-process.ts:209).
  3. processArguments adds -ApplePersistenceIgnoreState YES on darwin, after the app's own arguments, because Electron takes the first argument that is not a switch as the app's path (packages/electron/src/launcher.ts:40). macOS reads the setting only as two arguments, so the app's process.argv ends with both, -ApplePersistenceIgnoreState and YES. On Electron 45.5.0, systemPreferences.getUserDefault reads nothing for -ApplePersistenceIgnoreState=YES. The reference's launch-arguments table lists the switch and says the app sees both words.
  4. quotedLine quotes a stderr line whole up to 500 characters, and gives only the length of a longer one (app-process.ts:480). The reference's APP_NOT_OPEN row says so.
  5. endAttempt clears the attempt and the dialog handlers before it removes the user-data directory, so a removal that throws leaves no state behind (packages/electron/src/surface.ts:134). DialogRouter.reset() bumps a generation, and a handler of an earlier generation reports nothing (packages/electron/src/dialogs.ts:37-55).
  6. The integration tests' window helper kills its Electron when no window opens within 20 s, under vitest's hook timeout (packages/electron/tests/integration/test-window.ts).

Verified

Ran it locally: yes, on macOS (Apple Silicon) with Electron 45.5.0.

  • At 2b89ed4eb this branch passed the Electron package's unit and integration tests, the Electron testbed run through the built CLI (pnpm --filter @e2e-dev/electron-testbed test), and pnpm check. After the stderr change at head it passes the unit tests, tests/integration/launch.test.ts, typecheck, and oxlint.
  • main against this branch: this branch's tests run against origin/main's packages/electron/src. These tests fail there and pass here:
SuiteTestmainthis PR
unitDialogRouter keeps a handler that outlives its attempt from failing the next onefailspasses
integrationlaunch quotes no part of a long stderr line, which could cut a secret that holds white spacefailspasses
integrationlaunch reports why a launch failed within the operation's time, for an app whose main process is stuckfailspasses

The macOS launch argument has its own unit test (packages/electron/tests/unit/launch.test.ts); it is not in the table, because the local runs started main's Electron with the argument too. A launch whose window loaded just as its wait ran out has no test: it needs the load to land inside that last millisecond.

  • Benchmark: pnpm bench:ab --suite electron-testbed, the merge base with main against this head, on a Mac. This head starts the app with -ApplePersistenceIgnoreState; the base was given the same line locally and uncommitted, since without it a launch after a killed app stalls 120 s on the reopen-windows alert, which would swamp the timings.
    Result: the suite and every step are the same (suite -0.1%, 95% CI -0.4% to +0.2%), and the step counts match. One fixture test (mouse moves) changes its step sequence between runs of the same build, as it does on main, so it is noise.
bench:ab output

bench-ab: 7c1b2b25 (merge base with origin/main) vs 718c2948

apps/electron-testbed: e2e run --workers 1. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 26.13.1, load 2.8.

Behavior

These tests are left out of the timing rows below.

  • unstable on head (2 different step sequences) tests/fixture.e2e.ts › electron.keyboard and a window's page › moves, presses, and scrolls with the mouse where the test points it (electron)

Timings

Every row but the process wall clock covers the 86 tests whose behavior matched.

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)66.13 s (66.07 s-66.17 s)66.05 s (66.00 s-66.08 s)-0.1% (-0.5% to +0.3%)same
suite (sum of attempts)65.21 s (65.13 s-65.25 s)65.12 s (65.04 s-65.16 s)-0.1% (-0.4% to +0.2%)same
action (events)17.03 s (17.03 s-17.04 s)17.02 s (17.01 s-17.03 s)-0.1% (-0.2% to +0.0%)same
step app.open25.50 s (25.43 s-25.50 s)25.45 s (25.33 s-25.50 s)-0.2% (-1.0% to +1.0%)same
step locator.selectOption4.73 s (4.73 s-4.74 s)4.73 s (4.73 s-4.73 s)-0.0% (-0.1% to +0.0%)same, under the floor
step locator.tap4.42 s (4.41 s-4.42 s)4.42 s (4.40 s-4.42 s)-0.0% (-0.3% to +0.3%)same, under the floor
step locator.fill3.40 s (3.40 s-3.40 s)3.40 s (3.40 s-3.40 s)-0.1% (-0.3% to +0.1%)same, under the floor
step app.restart2.11 s (2.11 s-2.18 s)2.10 s (2.10 s-2.11 s)-0.2% (-2.5% to +0.4%)same, under the floor
step locator.dragTo1.44 s (1.43 s-1.44 s)1.44 s (1.43 s-1.44 s)-0.0% (-0.7% to +0.5%)same, under the floor
step locator.pressSequentially1.09 s (1.09 s-1.10 s)1.09 s (1.09 s-1.09 s)-0.2% (-0.6% to +1.2%)same, under the floor
step window.title1.00 s (1.00 s-1.01 s)1.00 s (1.00 s-1.01 s)+0.0% (-0.1% to +0.3%)same, under the floor
step electron.window956 ms (955 ms-956 ms)956 ms (955 ms-956 ms)+0.1% (-0.2% to +0.3%)same, under the floor
step electron.waitForWindow935 ms (933 ms-936 ms)937 ms (935 ms-946 ms)+0.6% (-0.1% to +2.4%)same, under the floor
step locator.scrollIntoView932 ms (932 ms-932 ms)931 ms (931 ms-931 ms)-0.1% (-0.3% to 0.0%)same, under the floor
step electron.launch917 ms (915 ms-917 ms)916 ms (915 ms-921 ms)-0.2% (-9.6% to +5.9%)same, under the floor
step locator.longPress905 ms (904 ms-906 ms)904 ms (904 ms-905 ms)-0.1% (-0.3% to +0.1%)same, under the floor
step locator.waitFor903 ms (903 ms-904 ms)903 ms (903 ms-904 ms)+0.1% (-0.1% to +0.2%)same, under the floor
step expect.toHaveValue776 ms (768 ms-782 ms)775 ms (774 ms-779 ms)+0.0% (-1.7% to +1.7%)same, under the floor
step locator.setInputFiles598 ms (595 ms-600 ms)595 ms (589 ms-597 ms)-0.3% (-2.4% to +1.2%)same, under the floor
step expect.toBeVisible530 ms (522 ms-533 ms)520 ms (517 ms-520 ms)-2.2% (-4.1% to +0.5%)same
step electron.waitForDownload525 ms (524 ms-526 ms)525 ms (524 ms-526 ms)-0.1% (-1.4% to +0.9%)same, under the floor
step expect.not.toHaveValue518 ms (517 ms-519 ms)519 ms (518 ms-520 ms)+0.3% (-0.2% to +1.1%)same, under the floor
step electron.evaluate509 ms (507 ms-512 ms)500 ms (498 ms-502 ms)-2.0% (-3.9% to +0.1%)same
step expect.toHaveText356 ms (354 ms-358 ms)348 ms (342 ms-351 ms)-2.4% (-7.5% to -0.3%)same, under the floor
step app.clearState339 ms (339 ms-339 ms)339 ms (338 ms-339 ms)0.0% (-0.6% to +0.4%)same
step locator.check326 ms (324 ms-329 ms)322 ms (320 ms-323 ms)-1.7% (-2.4% to -0.0%)same, under the floor
step app.screenshot123 ms (118 ms-128 ms)124 ms (115 ms-132 ms)+2.1% (-11.6% to +12.6%)same, under the floor
step app.stopRecording114 ms (111 ms-118 ms)111 ms (110 ms-113 ms)+0.5% (-20.3% to +4.3%)same, under the floor
step expect.toHaveCount106 ms (104 ms-109 ms)111 ms (108 ms-112 ms)+3.8% (+0.0% to +13.1%)same, under the floor
step locator.clear52 ms (51 ms-52 ms)51 ms (50 ms-52 ms)0.0% (-6.6% to +1.0%)same
step locator.press51 ms (49 ms-51 ms)50 ms (49 ms-50 ms)-2.0% (-3.9% to +6.0%)same, under the floor
step electron.keyboard.press50 ms (49 ms-50 ms)48 ms (47 ms-52 ms)-2.1% (-5.0% to +3.9%)same, under the floor
step locator.focus43 ms (42 ms-45 ms)42 ms (41 ms-43 ms)-2.2% (-6.8% to +1.2%)same, under the floor
step locator.swipe35 ms (35 ms-37 ms)36 ms (34 ms-36 ms)+0.0% (-6.9% to +15.0%)same, under the floor
step app.startRecording34 ms (34 ms-34 ms)34 ms (34 ms-34 ms)0.0% (-14.4% to +4.3%)same
step expect.toBeChecked34 ms (33 ms-34 ms)33 ms (33 ms-33 ms)-2.9% (-7.1% to +1.6%)same, under the floor
step electron.keyboard.type23 ms (22 ms-23 ms)23 ms (22 ms-23 ms)0.0% (-4.3% to +4.5%)same
step expect.toBeDisabled22 ms (22 ms-23 ms)23 ms (21 ms-23 ms)0.0% (-6.6% to +4.5%)same
step expect.toBeHidden21 ms (21 ms-21 ms)22 ms (21 ms-22 ms)+2.4% (-2.1% to +4.8%)same, under the floor
step locator.uncheck20 ms (18 ms-21 ms)20 ms (19 ms-20 ms)-4.8% (-5.1% to +14.2%)same, under the floor
step expect.toBeEditable20 ms (19 ms-20 ms)19 ms (18 ms-19 ms)-2.5% (-12.5% to +2.6%)same, under the floor
step expect.not.toBeChecked19 ms (19 ms-19 ms)19 ms (17 ms-19 ms)-7.4% (-13.2% to +8.5%)same, under the floor
step expect.toBeInViewport16 ms (16 ms-17 ms)16 ms (16 ms-17 ms)-2.8% (-9.3% to +6.5%)same, under the floor
step electron.keyboard.down15 ms (14 ms-17 ms)14 ms (14 ms-15 ms)-3.4% (-14.8% to 0.0%)same, under the floor
step electron.keyboard.up15 ms (14 ms-15 ms)15 ms (14 ms-15 ms)0.0% (-6.5% to +11.2%)same
step expect.toHaveAttribute14 ms (14 ms-14 ms)15 ms (15 ms-16 ms)+7.1% (-3.6% to +17.8%)same, under the floor
step expect.not.toBeInViewport14 ms (13 ms-15 ms)14 ms (13 ms-14 ms)0.0% (-13.3% to +11.8%)same
step locator.dblclick12 ms (12 ms-13 ms)13 ms (12 ms-13 ms)+4.1% (0.0% to +8.7%)same, under the floor
step expect.not.toBeEmpty11 ms (11 ms-11 ms)10 ms (10 ms-11 ms)-4.7% (-9.1% to 0.0%)same, under the floor
step expect.toBeEmpty11 ms (10 ms-11 ms)10 ms (10 ms-10 ms)-9.1% (-13.0% to 0.0%)same, under the floor
step locator.hover10 ms (10 ms-15 ms)10 ms (10 ms-14 ms)0.0% (-20.9% to +22.5%)same
step expect.not.toBeEditable10 ms (9 ms-10 ms)9 ms (8 ms-9 ms)-10.6% (-20.0% to 0.0%)same, under the floor
step window.reload9 ms (9 ms-9 ms)9 ms (9 ms-9 ms)0.0% (-5.1% to +5.4%)same
step electron.keyboard.insertText8 ms (8 ms-8 ms)8 ms (7 ms-8 ms)0.0% (-6.5% to 0.0%)same
step expect.toContainText8 ms (8 ms-8 ms)8 ms (8 ms-8 ms)0.0% (0.0% to 0.0%)same
step screen.tree6 ms (5 ms-6 ms)6 ms (6 ms-6 ms)0.0% (-15.5% to +29.6%)same
step app.back6 ms (6 ms-7 ms)6 ms (6 ms-6 ms)0.0% (-13.4% to 0.0%)same
step expect.not.toHaveAttribute6 ms (6 ms-6 ms)7 ms (6 ms-7 ms)0.0% (0.0% to +16.7%)same
step expect.not.toBeHidden6 ms (6 ms-6 ms)6 ms (6 ms-6 ms)0.0% (-8.7% to +9.5%)same
step expect.toHaveRole6 ms (5 ms-6 ms)5 ms (5 ms-5 ms)0.0% (-25.5% to 0.0%)same
step expect.toBeFocused5 ms (5 ms-6 ms)5 ms (4 ms-5 ms)-10.6% (-33.3% to +11.8%)same, under the floor
step expect.not.toHaveRole5 ms (5 ms-5 ms)5 ms (5 ms-5 ms)0.0% (-8.7% to +9.5%)same
step expect.toHaveAccessibleName5 ms (4 ms-5 ms)4 ms (4 ms-5 ms)0.0% (-20.0% to +11.8%)same
step expect.toBeSelected4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (0.0% to +11.8%)same
step window.evaluate4 ms (3 ms-5 ms)4 ms (3 ms-5 ms)0.0% (-51.0% to +66.7%)same
step expect.not.toBeEnabled4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (0.0% to +11.8%)same
step expect.toHaveScreenshot2 ms (1 ms-2 ms)1 ms (1 ms-1 ms)0.0% (-50.0% to 0.0%)same
step window.close1 ms (1 ms-1 ms)1 ms (1 ms-1 ms)0.0% (0.0% to +41.4%)same
step window.activate1 ms (0 ms-1 ms)0 ms (0 ms-1 ms)+41.4% (0.0% to +100.0%)same, under the floor
step electron.onDialog0 ms (0 ms-0 ms)1 ms (0 ms-1 ms)- (- to -)same, under the floor

Counters

Counterbasehead
events navigation123-131121-125
steps579579-580
Tests outside the threshold: 0 of 86

With 86 tests compared at 95%, about 4.3 could land outside by chance.

- Video: none attached. The change is in what the engine reads and when it acts, which the tests above pin down; a recording would show the same window either way. - macOS: before this branch, a local testbed run after an interrupted one stalled at launches for 120 s each, behind the system's reopen-windows alert. With the argument the testbed runs clean.

Fresh-context review (Codex gpt-6.2-sol, read-only, on this branch's diff):

  • Fixed: a cut at 500 characters could keep the start of a secret the app printed. Bot review then found that a cut at white space still splits a secret that holds white space, so a longer line now shows only its length (515881330).
  • Fixed: the failed launch could still wait up to 2 s for the killed app to exit and lose its reason to the operation's timeout; it now waits 250 ms and the next quit waits for the rest (aa55c2e8c, no test: it needs a process that exits slowly after a kill).
  • Fixed: added the changeset.
  • Dismissed: the reference lists APP_NOT_OPEN for a launch with no window, while the engine raises INVALID_STATE. Core maps INVALID_STATE to the test's APP_NOT_OPEN (packages/core/src/internal/errors.ts:381), which is the code a test sees.

Bot review (cubic, CodeRabbit) on 2b89ed4eb: five inline findings are left as is, as unlikely edge cases. They need less than 1.5 s of a 30 s default budget left at launch, a killed app that takes seconds to exit, rmSync failing after its retries, or a splash that closes during setup while its replacement is still loading. A sixth asked for a stricter assertion in the stderr test, which the new test replaces. CodeRabbit's summary also names Windows' taskkill (up to 5 s) on a failed launch's kill. That path is the same kill a failed launch reached on main when the app did not quit within its grace time. Each thread's reply gives the reason.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved launch-failure reporting when the app exits before opening a window or fails to load a page within the launch time.
    • Launch failures reserve time to gather diagnostic details and shut down the app; very long error lines are represented by their length.
    • Improved cleanup after failed launches and prevented dialog errors from an earlier attempt from affecting the next.
  • Documentation
    • Clarified launch-time failure cases, including apps waiting on a macOS keychain prompt.
    • Documented a macOS launch option that suppresses the reopen-windows prompt and noted that apps interpreting extra launch arguments as files or URLs should skip both related arguments.