fix(electron): end a video recording in its close time

Merged
KD
1d ago
+232-23

What

Ending an Electron video recording no longer hangs, and it leaves no broken file behind:

  • A recording whose ffmpeg stopped reading frames ends within its close time (10 s). It keeps what ffmpeg wrote, or fails with ENGINE_FAILURE when ffmpeg wrote nothing. It used to hold the end of the test for good.
  • A recording of an app whose main process is busy ends in a few seconds and keeps its file. The end of the screencast waited for an answer that only the main process gives.
  • A recording whose window never answers the start of its screencast (a page stuck in a loop) ends in about 2 s with no file.
  • A recording whose ffmpeg fails before it writes any video fails with ENGINE_FAILURE and leaves no empty .webm.
  • A final snapshot of a window that loaded another document while it was captured is dropped, so it cannot slip past the local file bound.

Why

Recorder.stop() and abandon() run at the end of every attempt that records, before the engine quits the app. Each of these could hang them or leave a bad artifact:

  • ffmpeg that stops reading fills its input pipe, and the next frame write waits for good. FrameEncoder.close() started its kill timer only after that write.
  • A window whose renderer does not answer Page.getLayoutMetrics or Page.startScreencast held the window switch that stop() awaited.
  • Page.stopScreencast had no bound. The browser's DevTools handlers run on Electron's main process, so a busy main process held it.
  • An ffmpeg that opens its output and then exits left a zero-byte file.
  • The bound was checked before the screenshot was taken, not after.

How it works

  1. withinCloseTime runs the whole end of a recording under one timer that kills the encoder after CLOSE_TIMEOUT_MS (packages/electron/src/video.ts:163). FrameEncoder.kill() also destroys the encoder's stdin, so a frame write waiting for room returns (video.ts:276).
  2. settleSwitching waits at most SWITCH_WAIT_MS (2 s) for a window switch in progress (video.ts:152). A switch that is answered later backs out once stopped is set: it starts no screencast on the next window when it was still stopping the window before (video.ts:74), it starts no encoder (video.ts:81), and it stops a screencast that started after the end.
  3. A source's stop() waits at most STOP_SCREENCAST_MS (2 s) for Page.stopScreencast (video.ts:100), so a busy main process cannot hold the encoder's close.
  4. When close() fails, the file is kept only if ffmpeg wrote something. A zero-byte file is removed and the recording fails with ENGINE_FAILURE (video.ts:138). A partial file from a killed ffmpeg is still kept, as on main.
  5. snap() checks again, after the capture, that the page's document generation did not change and that the page is still allowed (video.ts:183).
  6. Integration tests cover a failing encoder, a stuck encoder (a shell script that never reads), a window busy in while (true) {}, and a main process busy for 15 s (packages/electron/tests/integration/launch.test.ts:613-669). A unit test drives the late window switch with a scripted session (packages/electron/tests/unit/video.test.ts). The snapshot race in step 5 has no test: it needs a navigation that lands during one Page.captureScreenshot.

Verified

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

  • The head passes the Electron package's unit tests and tests/integration/launch.test.ts. 18b07de7e, before the review fixes, also passed the other integration tests, the Electron testbed run through the built CLI (pnpm --filter @e2e-dev/electron-testbed test), and pnpm check.
  • 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
integrationvideo fails, and keeps no file, when the encoder fails before it writes any videofailspasses
integrationvideo ends, in its close time, a recording whose encoder stopped readingfailspasses
integrationvideo ends a recording whose window never answers the start of its screencastfailspasses
integrationvideo ends a recording, in its close time, while the app's main process answers nothingfails (waits 15 s, until the main process answers)passes (about 4 s)
unitRecorder starts no screencast for a window switch that the end of the recording left waitingfails (stop() waits on the switch for good)passes

The snapshot check in step 5 has no test: it needs a navigation that lands during one Page.captureScreenshot.

  • Benchmark: pnpm bench:ab --suite electron-testbed, the merge base with main against this head, on a Mac. Both builds ran with -ApplePersistenceIgnoreState (#2500) applied locally and uncommitted, since without it a launch after a killed app stalls 120 s on the reopen-windows alert. The "uncommitted changes" in the header are that line.
    Result: the suite and every step are the same (the suite +0.3%, 95% CI -0.0% to +0.8%), and the step counts match. One fixture test changes its step sequence between two runs of the same build, on both base and head, so it is noise and not this change.
bench:ab output

bench-ab: 7c1b2b25 (merge base with origin/main) vs d20f0bae + uncommitted changes

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 4.6.

Behavior

These tests are left out of the timing rows below.

  • unstable on base (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)
  • 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)63.76 s (63.68 s-63.86 s)63.91 s (63.77 s-64.17 s)+0.3% (-0.2% to +0.8%)same
suite (sum of attempts)62.90 s (62.85 s-62.99 s)63.04 s (62.95 s-63.33 s)+0.3% (-0.0% to +0.8%)same
action (events)16.87 s (16.86 s-16.87 s)16.86 s (16.86 s-16.87 s)+0.0% (-0.1% to +0.1%)same, under the floor
step app.open24.37 s (24.32 s-24.45 s)24.40 s (24.39 s-24.43 s)+0.3% (-0.5% to +0.5%)same
step locator.selectOption4.72 s (4.72 s-4.73 s)4.72 s (4.72 s-4.73 s)+0.0% (-0.1% to +0.2%)same, under the floor
step locator.tap4.37 s (4.37 s-4.38 s)4.38 s (4.37 s-4.38 s)+0.0% (-0.2% to +0.3%)same, under the floor
step locator.fill3.37 s (3.36 s-3.37 s)3.37 s (3.36 s-3.37 s)-0.1% (-0.1% to +0.2%)same, under the floor
step app.restart1.98 s (1.97 s-1.99 s)1.97 s (1.97 s-1.98 s)-0.4% (-1.0% to +0.3%)same, under the floor
step locator.dragTo1.43 s (1.42 s-1.43 s)1.43 s (1.43 s-1.43 s)-0.1% (-0.2% to +0.2%)same, under the floor
step locator.pressSequentially1.08 s (1.08 s-1.09 s)1.08 s (1.08 s-1.08 s)+0.0% (-0.4% to +0.3%)same, under the floor
step window.title1.00 s (1.00 s-1.00 s)1.00 s (1.00 s-1.00 s)+0.0% (-0.3% to +0.1%)same, under the floor
step electron.window955 ms (953 ms-956 ms)957 ms (956 ms-957 ms)+0.1% (+0.1% to +0.3%)same, under the floor
step electron.waitForWindow934 ms (925 ms-935 ms)936 ms (931 ms-940 ms)+0.4% (-0.5% to +1.5%)same, under the floor
step locator.scrollIntoView927 ms (925 ms-927 ms)926 ms (925 ms-928 ms)+0.1% (-0.1% to +0.1%)same, under the floor
step electron.launch913 ms (912 ms-913 ms)913 ms (912 ms-914 ms)0.0% (-0.1% to +0.3%)same
step locator.longPress904 ms (903 ms-905 ms)904 ms (903 ms-904 ms)-0.1% (-0.2% to +0.2%)same, under the floor
step locator.waitFor903 ms (902 ms-904 ms)903 ms (902 ms-903 ms)-0.1% (-0.1% to +0.1%)same, under the floor
step expect.toHaveValue744 ms (742 ms-746 ms)745 ms (739 ms-751 ms)-0.1% (-1.1% to +1.1%)same, under the floor
step locator.setInputFiles570 ms (570 ms-571 ms)568 ms (568 ms-569 ms)-0.4% (-0.6% to -0.1%)same, under the floor
step electron.waitForDownload517 ms (516 ms-518 ms)517 ms (516 ms-518 ms)-0.1% (-0.3% to +2.2%)same, under the floor
step expect.not.toHaveValue514 ms (512 ms-517 ms)518 ms (515 ms-519 ms)+0.5% (0.0% to +1.3%)same, under the floor
step electron.evaluate479 ms (477 ms-484 ms)484 ms (482 ms-491 ms)+0.4% (-1.6% to +4.7%)same, under the floor
step expect.toBeVisible436 ms (420 ms-443 ms)439 ms (435 ms-441 ms)+0.1% (-4.8% to +7.9%)same, under the floor
step app.clearState335 ms (332 ms-338 ms)338 ms (336 ms-338 ms)+0.4% (-1.2% to +2.4%)same, under the floor
step expect.toHaveText328 ms (325 ms-330 ms)327 ms (324 ms-332 ms)-0.5% (-2.3% to +2.4%)same, under the floor
step locator.check312 ms (311 ms-314 ms)312 ms (309 ms-314 ms)-0.3% (-2.2% to +2.3%)same, under the floor
step app.screenshot121 ms (114 ms-123 ms)125 ms (120 ms-130 ms)+0.4% (-3.4% to +13.7%)same, under the floor
step expect.toHaveCount96 ms (94 ms-99 ms)99 ms (97 ms-101 ms)+1.0% (-1.0% to +5.9%)same, under the floor
step app.stopRecording90 ms (83 ms-94 ms)88 ms (87 ms-89 ms)-2.7% (-8.5% to +11.4%)same, under the floor
step locator.press50 ms (48 ms-50 ms)49 ms (49 ms-50 ms)+1.0% (-3.8% to +3.2%)same, under the floor
step locator.clear47 ms (46 ms-48 ms)47 ms (46 ms-48 ms)0.0% (-3.2% to +2.2%)same
step locator.focus35 ms (34 ms-36 ms)35 ms (34 ms-36 ms)0.0% (-2.9% to +1.5%)same
step electron.keyboard.press33 ms (32 ms-34 ms)34 ms (33 ms-36 ms)+4.5% (0.0% to +10.8%)same, under the floor
step expect.toBeChecked31 ms (29 ms-31 ms)31 ms (29 ms-32 ms)-1.6% (-5.2% to +8.6%)same, under the floor
step locator.swipe30 ms (29 ms-35 ms)35 ms (29 ms-36 ms)-2.8% (-7.8% to +22.0%)same, under the floor
step app.startRecording25 ms (24 ms-29 ms)25 ms (25 ms-25 ms)+2.1% (-18.0% to +12.0%)same, under the floor
step expect.toBeHidden22 ms (21 ms-22 ms)21 ms (21 ms-21 ms)-4.5% (-4.5% to +2.5%)same, under the floor
step expect.toBeDisabled20 ms (19 ms-20 ms)20 ms (19 ms-21 ms)0.0% (-10.0% to +7.9%)same
step electron.keyboard.type20 ms (19 ms-20 ms)21 ms (19 ms-21 ms)+2.5% (-2.4% to +5.0%)same, under the floor
step expect.not.toBeChecked18 ms (17 ms-20 ms)19 ms (18 ms-20 ms)+5.7% (-13.4% to +14.9%)same, under the floor
step expect.toBeEditable18 ms (17 ms-19 ms)16 ms (16 ms-18 ms)-8.5% (-16.2% to +0.2%)same, under the floor
step locator.uncheck16 ms (15 ms-17 ms)16 ms (15 ms-16 ms)-3.0% (-11.6% to +3.3%)same, under the floor
step expect.toHaveAttribute13 ms (12 ms-13 ms)13 ms (12 ms-13 ms)+3.8% (-11.6% to +13.2%)same, under the floor
step locator.dblclick12 ms (12 ms-12 ms)12 ms (11 ms-12 ms)0.0% (-11.9% to +4.1%)same
step expect.toBeInViewport12 ms (11 ms-12 ms)10 ms (10 ms-10 ms)-9.1% (-16.7% to 0.0%)same, under the floor
step electron.keyboard.down11 ms (10 ms-11 ms)11 ms (10 ms-11 ms)-4.7% (-9.1% to +14.9%)same, under the floor
step electron.keyboard.up10 ms (10 ms-11 ms)10 ms (9 ms-10 ms)-9.5% (-14.2% to 0.0%)same, under the floor
step expect.not.toBeEmpty10 ms (10 ms-11 ms)9 ms (9 ms-9 ms)-10.0% (-14.2% to -9.5%)same, under the floor
step expect.toBeEmpty10 ms (10 ms-11 ms)10 ms (9 ms-10 ms)-9.5% (-19.1% to 0.0%)same, under the floor
step locator.hover10 ms (9 ms-12 ms)13 ms (10 ms-15 ms)+10.6% (-13.4% to +52.8%)same, under the floor
step expect.not.toBeInViewport9 ms (9 ms-10 ms)9 ms (9 ms-9 ms)0.0% (-14.7% to 0.0%)same
step expect.not.toBeEditable9 ms (8 ms-9 ms)9 ms (8 ms-10 ms)+11.1% (-5.7% to +12.5%)same, under the floor
step window.reload8 ms (7 ms-8 ms)8 ms (8 ms-8 ms)0.0% (0.0% to +14.3%)same
step expect.toContainText7 ms (7 ms-7 ms)8 ms (7 ms-9 ms)+14.3% (0.0% to +26.8%)same, under the floor
step expect.not.toHaveAttribute6 ms (6 ms-6 ms)6 ms (6 ms-6 ms)0.0% (-15.5% to +8.0%)same
step electron.keyboard.insertText5 ms (5 ms-6 ms)6 ms (5 ms-6 ms)0.0% (-8.7% to +20.0%)same
step app.back5 ms (4 ms-10 ms)4 ms (4 ms-4 ms)-20.0% (-66.6% to +22.5%)same, under the floor
step expect.not.toBeHidden5 ms (5 ms-5 ms)6 ms (6 ms-6 ms)+20.0% (+9.5% to +20.0%)same, under the floor
step screen.tree5 ms (4 ms-5 ms)5 ms (4 ms-5 ms)0.0% (0.0% to +11.8%)same
step expect.not.toHaveRole5 ms (4 ms-5 ms)4 ms (4 ms-5 ms)0.0% (-20.0% to +11.8%)same
step expect.toHaveRole5 ms (4 ms-5 ms)5 ms (5 ms-5 ms)+22.5% (-10.6% to +44.3%)same, under the floor
step expect.toBeFocused4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (-13.4% to +15.5%)same
step expect.toHaveAccessibleName4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (-13.4% to 0.0%)same
step expect.toBeSelected4 ms (4 ms-4 ms)4 ms (3 ms-4 ms)0.0% (-22.5% to 0.0%)same
step window.evaluate4 ms (3 ms-5 ms)4 ms (3 ms-4 ms)-10.6% (-36.8% to +15.5%)same, under the floor
step expect.not.toBeEnabled4 ms (3 ms-4 ms)4 ms (3 ms-4 ms)0.0% (-13.4% to +15.5%)same
step expect.toHaveScreenshot1 ms (1 ms-1 ms)2 ms (1 ms-2 ms)+41.4% (0.0% to +100.0%)same, under the floor
step window.close1 ms (1 ms-1 ms)2 ms (1 ms-2 ms)+41.4% (-29.3% to +100.0%)same, under the floor

Counters

Counterbasehead
events navigation114-121113-124
steps579-580579-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. - Local macOS runs, on both sides, started Electron with `-ApplePersistenceIgnoreState YES` from #2500, applied without committing it. Without it, an Electron the tests killed makes macOS ask to reopen its windows, and the next launch waits out its timeout. The Linux and Windows CI jobs do not need it.

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

  • Fixed: a window that never answers the start of its screencast held stop() and the app's quit for good; the wait for a window switch is now bounded (effdb37de, with a test).
  • Fixed: added the changeset.
  • Dismissed: an ffmpeg killed after it wrote the WebM header but before any frame keeps a file with no frames. That needs an encoder that stalls before its first frame; a recording killed later plays up to its last complete cluster, which is the reason a partial file is kept.

Bot review threads, on 18b07de7e:

  • Fixed: Page.stopScreencast had no bound, so a busy main process held stop() past its close time (7c920cc97, with a test). A busy renderer does not: the browser process answers the stop, measured at 0 ms with the page in while (true) {}.
  • Fixed: a window switch that the end of the recording left waiting on the stop of the window before started a screencast on the next window (a55cb4fde, with a test).
  • Dismissed: a killed ffmpeg that wrote part of the file returns it as the recording. main does the same, and a partial WebM plays up to its last complete cluster.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Video recordings now stop reliably when a window or recording process becomes unresponsive.
    • Recording failures are reported when no video is produced, and failed recordings no longer leave empty video files behind.
    • Nonempty partial recordings are preserved when finalization fails.
    • Captures are discarded if a page changes during capture or is outside the app’s allowed directory.