fix(electron): download the Electron binary one process at a time
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 binaryand starts its tests once the binary is complete. The Electron guide says so. - This covers runs in one project and projects whose
node_modules/electronlinks 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.txtfrom an earlier install is still there. - A run whose
ELECTRON_OVERRIDE_DIST_PATHleads back into the package's owndistwaits for that download too. That is an override that namesdist, 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 saidinstall.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_FAILEDandfailed to create directory ...: File exists (os error 17). - A
path.txtis left from an earlier install and another run is extracting.install.jswrites the main binary early andElectron Frameworklate, and the engine calls the binary ready oncepath.txtand 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]
downloadstartsinstall-processwhere it startedinstall.js(packages/electron/src/executable.ts:259), still one process. It picksdist/install-process.jswhen built and the.tsbeside it from source (executable.ts:241), aspackages/e2e/src/run/worker/handle.tsdoes for the worker entry. The process is started withoutELECTRON_OVERRIDE_DIST_PATH, becauseinstall.jswould otherwise look forelectron.d.tsin the override directory and move it into the package.install-process.tstakes the lock (packages/electron/src/install-process.ts:16). Whenpath.txtnames no binary that is there, it createsdist, writes.e2e-install.incompletebesideinstall.jsand removespath.txt(install-process.ts:30). Then it loadsinstall.js.install.jswritespath.txtlast, so while a download writes there is nopath.txtto call its binary ready.install.jsexits at once when its own check finds the binary installed, which is the second check a process that waited needs.- The engine reads
path.txtagain 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 oldpath.txtwith the binary the writer had just created. - A binary that
ELECTRON_OVERRIDE_DIST_PATHnames is taken as it stands, since no download writes it. With nopath.txt, the override lookup tries the platform's own binary name afterelectron(executable.ts:77), because another project's download removes the file that lookup took the name from. - One kind of override is not taken as it stands. While
.e2e-install.incompleteis there, an override that leads back into the package'sdistis 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 namesdist, one that is a link to it, and one whose binary is really a file inside it. - The process releases the lock when it exits, and clears
.e2e-install.incompleteafter 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). holdInstallLockcreates the lock withwxbesideinstall.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.- 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. - 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.breakfile 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-electronandrequire('electron')take no lock. - A binary that is already truncated, with a
path.txtthat 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.txtand 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.lockin 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.incompleteuntil 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 setsELECTRON_OVERRIDE_DIST_PATH, links to the same package and relied on apath.txtthat named another platform's binary getsINVALID_CONFIGuntil a download completes. Onmainapath.txtleft behind kept that project working. - An override is followed back into the package's
distthrough 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 withINVALID_CONFIG, as onmain. So does an override of adistthat 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.
| Case | main | this PR |
|---|---|---|
| Two prepare steps start together, binary missing | 5 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 extracts | 5 of 5: the second returns with Electron Framework at 36 to 37 MB of 207 | 5 of 5: the second waits and returns on the complete file |
| The same, and the writing process gets SIGTERM. A third prepare step runs after | 3 of 3: the second and the third return on an incomplete framework | 3 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 run | 3 of 3: the next run downloads again and completes | 3 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 missing | not run | 5 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 whoseElectron.appis 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.appis 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 inbinary-download.test.ts, 2 inexecutable.test.ts), 7 are guards that pass there and say so in their titles, and the 14 ininstall-lock.test.tscannot 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 whoseElectron.appis a link intodistand which holds anelectron.d.tsof its own. That file and the package'selectron.d.tsstay as they were, 2 of 2. In a build that passes the variable on toinstall.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-bookwormon 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-windowsjob 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:abtable: the change runs only when the binary is missing, wherepreparestartsinstall-processin place ofinstall.js. With the binary in place it readspath.txtonce 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.