fix(electron): delete app data once a lingering Windows app lets go
What
- On Windows, a test no longer ends with
cleanup failed: ENGINE_FAILURE: attempt end failed: EPERM, Permission denied: '...\Temp\e2e-electron-XXXX'when the app's processes still hold files in its data directory. The directory is deleted with a later test's, or when the worker ends. app.clearState()starts the app on a new, empty data directory instead of emptying the old one.- The engine no longer runs
taskkillon an app that already exited. - The Electron tests pass on a plain or slow Windows machine: no
ls, a real "no process left" check, and no 1 s assumptions about crash and exit notices.
Why
Measured on a Windows Server 2025 machine (2 vCPU). An Electron app exits about 200 ms after it quits when the machine is idle. Under CPU load its DevTools pipe closes after about 0.5 s, but its process exits about 14 s later, with or without --inspect.
The engine waits 1 s for the exit, runs taskkill /T /F, waits 2 s more, then deleted the data directory with rmSync (5 retries, 1.5 s). Windows refuses to delete a file a process holds open, so the delete failed, the attempt's cleanup failed, and the runner retired the worker. The first testbed run on a fresh machine (cold disk, antivirus scanning new binaries) hit this 4 times with no added load.
Three smaller things from the same runs:
- Every app exit ran
taskkill /pid <exited pid> /T /Fsynchronously. It always exits 128 (The process "6980" not found), reaches no surviving child, and blocks the runner for 40 ms idle and 340 ms under load. quits the app through its own quit handlersshells out tols, so the integration suite fails outside Git Bash:spawnSync ls ENOENT. CI hides it because the job's shell is bash.processesOfreturned[]on Windows, so "leaves no process" checked nothing there.
How it works
AppDataowns the data directories.discarddeletes one with the same retries as before. On Windows, a delete that fails withEBUSY,EPERM, orENOTEMPTYkeeps the directory for later instead of failing. Any other failure, on any platform, still fails the attempt. Eachdiscardfirst tries the kept ones once, without waiting.packages/electron/src/app-data.ts:47AppData.closeruns at the worker's end. It retries the kept directories until the dispose budget is over, then fails withENGINE_FAILUREnaming the ones still there, so app data never stays behind silently.app-data.ts:61ElectronSurface.endAttemptdiscards through it, anddisposecloses it after the attempt's own cleanup. The attempt's failure wins when both fail.packages/electron/src/surface.ts:138,surface.ts:149reset(app.clearState()) makes a new directory and discards the old one. It used tormSyncthe directory the quitting app could still hold, which would fail the test with the sameEPERM.surface.ts:175killGroupreturns early on Windows when the child has an exit code.packages/electron/src/launcher.ts:174- Tests.
processesOfasks PowerShell for processes whose command line names the app's directory (the directory goes in by the environment, so the query does not match itself). The testbed's crash test gives the crash notice 10 s, since Chromium reports it after writing a dump. The quit test polls until the engine reports the app gone, which it does when the process exits.
Dropped: treating the closed DevTools pipe as the end of the quit and killing right away. It would save the grace wait on a loaded Windows machine, but I could not show Chromium has flushed the profile by then, and app.restart() keeps that data.
Issue
None filed. Found in a Windows verification pass of @e2e-dev/electron.
Verified
Ran it locally: yes, on a Windows Server 2025 machine in an interactive desktop session, and the unit tests on macOS.
pnpm --filter @e2e-dev/electron-testbed test:
| Run | main | this PR |
|---|---|---|
| Idle machine | 75 passed | 75 passed |
| First run on a fresh machine | 2 failed, 4 cleanup failures (EPERM) | not repeatable (machine is warm) |
| 3 CPU-bound processes on 2 vCPU | 4 failed, 2 cleanup failures (EPERM), 2 data directories left in %TEMP% | 2 and 3 failed in two runs, 0 cleanup failures, 0 directories left |
The failures left under load are timing assertions the load itself breaks, a different set each run (app.restart() under 4 s, a 1.5 s drag, a 1 s visibility wait). The crash and quit tests this PR changes failed on main in both loaded runs and pass here in both.
pnpm --filter @e2e-dev/electron run test:integration on Windows, from cmd rather than Git Bash:
| main | this PR |
|---|---|
1 failed: spawnSync ls ENOENT | 44 passed, 1 skipped |
main, under load:
Cleanup 2 cleanup failures: ENGINE_FAILURE: attempt end failed: EPERM, Permission denied: 'C:\Users\ADMINI~1\AppData\Local\Temp\e2e-electron-57jvrM' (+1 more)
quit timeline under load (plain Electron 45.5.0, ms after app.quit()):
101 stderr: WILL-QUIT
566 cdp pipe end
14149 process exit code=0
taskkill on an exited root:
taskkill dead parent: status 128 ERROR: The process "6980" not found. 43 ms; pings alive: 1
Unit tests: 103 passed on Windows and macOS. pnpm check passes.
No benchmark table: the change is in attempt cleanup, and the idle run takes 89 s on both. No video: nothing on screen changes.