fix(electron): download the Electron binary one process at a time

MI
4h ago
+943-15

What

  • Runs that need the Electron binary at the same time wait for one download. A run that waits prints waiting for another run that is downloading the Electron binary and starts its tests once the binary is complete. The Electron guide says so.
  • This covers runs in one project and projects whose node_modules/electron links to one package, such as a pnpm workspace or a scratch project that takes its dependencies from a checkout.
  • A download that was cancelled or killed is downloaded again by the next run, also when a path.txt from an earlier install is still there.
  • A run whose ELECTRON_OVERRIDE_DIST_PATH leads back into the package's own dist waits for that download too. That is an override that names dist, a link to it, or a directory whose binary is a link into it. An override that holds its own binary starts at once, as before.
  • A download whose process was killed says which signal ended it: install.js was killed (SIGKILL), where it said install.js exited with null.

Why

Electron 45 has no install script, so prepare runs the package's install.js when the binary is missing. Nothing kept a second process from running it too, and each extracted over the other. In the trials under Verified, with the 45.5.0 archive on macOS, main did this in every trial:

  • Two runs start with no binary. One of them fails with ELECTRON_INSTALL_FAILED and failed to create directory ...: File exists (os error 17).
  • A path.txt is left from an earlier install and another run is extracting. install.js writes the main binary early and Electron Framework late, and the engine calls the binary ready once path.txt and the binary exist. The second run goes on while the framework is still being written. If the first download is then cancelled, later runs accept that binary too, with its framework incomplete.

I hit it once outside a test. An e2e run in a scratch project and the package's integration tests started together in a fresh worktree of #2439's head, whose prepare step is main's. 35 of the 63 integration tests failed, and the app printed dyld: Library not loaded: @rpath/Electron Framework.framework/Electron Framework. I did not reproduce that launch on main.

How it works

flowchart TD
  A[prepare] --> B{path.txt and the binary it names?}
  B -- yes --> R[return]
  B -- no --> S[start install-process]
  S --> L{lock}
  L -- free --> P[mark dist incomplete and remove a path.txt that names a missing binary]
  P --> I["run install.js, which exits at once when it finds the binary installed"]
  L -- "holder alive and touching it" --> W[wait 50 ms] --> L
  L -- "holder gone, or untouched for 10 s of waiting" --> T[take it over] --> L
  I --> X[release on exit, and clear the mark after a complete install]
  1. download starts install-process where it started install.js (packages/electron/src/executable.ts:259), still one process. It picks dist/install-process.js when built and the .ts beside it from source (executable.ts:241), as packages/e2e/src/run/worker/handle.ts does for the worker entry. The process is started without ELECTRON_OVERRIDE_DIST_PATH, because install.js would otherwise look for electron.d.ts in the override directory and move it into the package.
  2. install-process.ts takes the lock (packages/electron/src/install-process.ts:16). When path.txt names no binary that is there, it creates dist, writes .e2e-install.incomplete beside install.js and removes path.txt (install-process.ts:30). Then it loads install.js. install.js writes path.txt last, so while a download writes there is no path.txt to call its binary ready. install.js exits at once when its own check finds the binary installed, which is the second check a process that waited needs.
  3. The engine reads path.txt again once it has found the package's binary (executable.ts:83). A read that began before the writer removed the file would otherwise pair the old path.txt with the binary the writer had just created.
  4. A binary that ELECTRON_OVERRIDE_DIST_PATH names is taken as it stands, since no download writes it. With no path.txt, the override lookup tries the platform's own binary name after electron (executable.ts:77), because another project's download removes the file that lookup took the name from.
  5. One kind of override is not taken as it stands. While .e2e-install.incomplete is there, an override that leads back into the package's dist is read as the package's own binary, so its run waits for the download like any other (executable.ts:50, :80). That is an override that names dist, one that is a link to it, and one whose binary is really a file inside it.
  6. The process releases the lock when it exits, and clears .e2e-install.incomplete after a complete install (install-process.ts:22). It has no signal handler. A cancelled run's SIGTERM ends it within a few milliseconds, the lock file stays, and the next process takes it over because the pid it names is gone. When something else kills the process, the run that started it names the signal (executable.ts:269).
  7. holdInstallLock creates the lock with wx beside install.js, so every project that links to the package meets the same file (packages/electron/src/install-lock.ts:53, :82). The file names the holder's host, pid namespace and pid, and the holder touches it every 2.5 s.
  8. A waiter takes the lock over at once when that pid is gone on its own host. Otherwise it takes it over after it has polled the lock for 10 s without seeing it change (install-lock.ts:45). It counts its own polls and reads no clock, so a laptop that sleeps through a download or a host with a different time cannot make a live lock look abandoned.
  9. A takeover goes through a second file, .e2e-install.lock.break, and the waiter removes the lock only if it is still the one it watched (install-lock.ts:165). A .break file left behind is abandoned by the same rule as the lock (install-lock.ts:161).

The process that writes the binary holds the lock, not the run that started it. On macOS and Linux a run killed by a timeout leaves its install.js writing, and a lock held by the dead run would be taken over while that process still extracts.

Limits:

  • Electron's own npx install-electron and require('electron') take no lock.
  • A binary that is already truncated, with a path.txt that names it, is not repaired. The run under Why left one behind.
  • A lock holder that is stopped on its own for more than 10 s while another run waits (Ctrl+Z, a debugger) loses the lock, and both write once it resumes. The one that finishes first writes path.txt and clears .e2e-install.incomplete, so a run that arrives then can start while the other still writes.
  • A cancelled or killed download leaves .e2e-install.lock in the package until the next download takes it over. That is at once on the same host, and after 10 s when the lock names another host or a pid that is in use again. It leaves .e2e-install.incomplete until a download completes.
  • From the start of a download until one completes, the package has no path.txt, and a failed or cancelled download leaves it that way. A project that sets ELECTRON_OVERRIDE_DIST_PATH, links to the same package and relied on a path.txt that named another platform's binary gets INVALID_CONFIG until a download completes. On main a path.txt left behind kept that project working.
  • An override is followed back into the package's dist through its directory or its binary, and no further. A link whose target the extraction has not created yet is no binary, so that run stops with INVALID_CONFIG, as on main. So does an override of a dist that an interrupted download left missing, unless the variable spells the resolved path. A binary of its own whose framework is the link, a hard link, and a link written in another letter case on a case-insensitive disk are not seen, and such a run can start on a binary that is still being written.
  • Two waiters can both take the lock when a third process died inside a takeover and the two interleave within those few file calls. The login lock documents the same race (packages/e2e/src/internal/file-lock.ts:153).

For maintainers

install-lock.ts is a second copy of the lock e2e keeps for its login store (packages/e2e/src/internal/file-lock.ts), because an engine imports @e2e-dev/core/engine only. It differs in two rules, because it is held for a whole download: it puts no limit on the wait, where the login lock gives up after a set one, and it counts the waiter's polls, where the login lock reads staleness from the file's age. The alternative is one lock exported from @e2e-dev/core/engine, which adds to the engine contract. Tell me if you want that instead.

Two of the limits are left on purpose:

  • The stopped holder. The lock takes over after 10 s of silence, where it could make the waiting run fail instead. Taking over keeps a leftover lock from blocking every later run.
  • The takeover race. I know no way to close it with plain files. It needs a lock the operating system holds, which Node offers only through a native dependency.

Issue

None filed. Found in a review run of #2439, described under Why.

Verified

Ran it locally: yes, on macOS 27.2 (arm64) with Node 26.13.1 and the real Electron 45.5.0 archive. main is 7c1b2b258 and this PR is fa55008c0. One script produced both columns.

The first four rows run the built engine's prepare step in separate processes, for scratch projects that link to one copy of the electron package, and read Electron Framework on disk. The last row runs the built CLI in the checkout.

Casemainthis PR
Two prepare steps start together, binary missing5 of 5: one fails with ELECTRON_INSTALL_FAILED, File exists (os error 17)5 of 5: both exit 0 and one waits. Electron Framework is 207,467,680 bytes with the SHA-256 of a single install
path.txt left behind, second prepare step arrives while the first extracts5 of 5: the second returns with Electron Framework at 36 to 37 MB of 2075 of 5: the second waits and returns on the complete file
The same, and the writing process gets SIGTERM. A third prepare step runs after3 of 3: the second and the third return on an incomplete framework3 of 3: the writer is gone 3.4 to 4.6 ms after the signal, and the second, which was waiting, and the third return on the complete file
Writing process killed with SIGKILL, then the next run3 of 3: the next run downloads again and completes3 of 3: the kill leaves the lock, and the next run takes it over, completes the install and leaves no lock
e2e run on the Electron testbed and pnpm --filter @e2e-dev/electron test:integration, started together with the binary missingnot run5 of 5 rounds: both start on the complete binary, and 87 and 63 tests pass in every round. One run waits in 4 rounds. In the other neither waits. The testbed's prepare step returns in 94 ms there, against 643 ms in a round where it extracts

In the two kill rows the run that started the download reports install.js exited with null on main and install.js was killed (SIGTERM) or (SIGKILL) here.

The second round, the testbed run and then the integration run, which waited:

 RUN  e2e v0.25.0 .../apps/electron-testbed
ℹ downloading the Electron 45.5.0 binary
 ✓ electron engine for target "electron" prepared 643ms
...
 Test Files  4 passed (4)
      Tests  87 passed (87)

 RUN  v5.1.2 .../packages/electron
downloading the Electron 45.5.0 binary
waiting for another run that is downloading the Electron binary
...
 Test Files  2 passed (2)
      Tests  63 passed (63)

Overrides, on this PR, in the same setup while another step extracts. Each is 3 of 3, one of the three with a path.txt left behind:

  • The variable names the package's dist, a link to it, or a directory whose Electron.app is a link into it: the run waits and returns on the complete file.
  • The variable names a complete binary of its own, or a directory whose Electron.app is a link to one: the run returns at once on that binary.
  • A link whose target does not exist yet, with a stand-in installer that holds the fetch open: INVALID_CONFIG, 2 of 2, where a link to the whole directory waits.

Tests and the rest:

  • New unit tests: 35. On main, 14 fail on their assertion (12 in binary-download.test.ts, 2 in executable.test.ts), 7 are guards that pass there and say so in their titles, and the 14 in install-lock.test.ts cannot load because the module is new. The package's unit suite passes: 149 tests in 15 files.
  • 25 hand-written mutants of the three source files: 24 fail a test on an assertion. The other one ignores .e2e-install.incomplete. It fails the guard for a hand-supplied binary with the error of a download it should not have started, which is not an assertion.
  • The download starts without ELECTRON_OVERRIDE_DIST_PATH. After an interrupted download, a run downloads again through a directory whose Electron.app is a link into dist and which holds an electron.d.ts of its own. That file and the package's electron.d.ts stay as they were, 2 of 2. In a build that passes the variable on to install.js, the directory's file replaced the package's, 2 of 2.
  • The three test files that hold them pass 20 of 20 times with every core busy.
  • Linux: the package's unit suite passes in node:22.22.3-bookworm on arm64 (149 tests), and those three test files also as a user other than root (42 tests).
  • Windows and Bun were not run locally. The electron-windows job runs the unit tests, the integration tests and the testbed on Windows. Two new tests are skipped there. Windows ends a run's child processes with the run, and it reports a process that another one killed as an exit with code 1, so there is no signal to name.
  • No pnpm bench:ab table: the change runs only when the binary is missing, where prepare starts install-process in place of install.js. With the binary in place it reads path.txt once more, and for an override it looks for .e2e-install.incomplete.
  • No terminal recording: the change is one log line and one error text, both quoted above. No docs screenshot: the page gains two sentences.

Summary by CodeRabbit

  • Bug Fixes
    • Concurrent test runs that need the same Electron binary now wait for a shared download to finish, reducing installation failures and incomplete binaries.
    • Electron binary preparation can recover from interrupted downloads and recognize supported platform-specific binaries in override directories.
  • Documentation
    • Clarified when Electron binaries are downloaded and how runs sharing a package or using an override directory are affected.