Skip to content

fix(screencast): auto-save video when page closes without stop#41746

Open
Git-Raini wants to merge 2 commits into
microsoft:mainfrom
Git-Raini:fix-41608-screencast-autosave
Open

fix(screencast): auto-save video when page closes without stop#41746
Git-Raini wants to merge 2 commits into
microsoft:mainfrom
Git-Raini:fix-41608-screencast-autosave

Conversation

@Git-Raini

Copy link
Copy Markdown
Contributor

Closes #41608

This builds on @sarang-code2's #41744, which correctly diagnosed and fixed the original problem (video never saved if the page closes before screencast.stop() is called). Full credit to them for that work - this PR carries their commit as-is, plus one addition:

Original fix (unchanged from #41744): listen for Events.Page.Close in the client-side Screencast constructor, and auto-save the artifact if a recording is active with a path option.

Added on top: in review on #41744, I flagged that the auto-save was fire-and-forget (artifact.saveAs(savePath).catch(() => {}), never awaited) - so page.close() could resolve before the video was actually written to disk, and a failed save produced no signal at all. This PR makes Screencast track the in-flight save (_pendingAutoSave) and has Page.close() await it in a finally block. Verified safe by tracing PageDispatcher: the server dispatches the 'close' event (which triggers the auto-save) before it sends the response to the close() call, so the pending-save promise is always already assigned by the time close() reaches its finally.

Full discussion: #41744 (comment) and sarang-code2#1 (opened against their branch first, before this one).

Test results - screencast.spec.ts, 195 runs (13 tests × 15 repeats), all passing
[182/195] [chromium-library] › tests\library\screencast.spec.ts:237:5 › start dispose stops recording
[183/195] [chromium-library] › tests\library\screencast.spec.ts:28:5 › screencast.start delivers frames via onFrame callback
[184/195] [chromium-library] › tests\library\screencast.spec.ts:57:5 › onFrame receives viewport size
[185/195] [chromium-library] › tests\library\screencast.spec.ts:108:5 › start returns a disposable that stops screencast
[186/195] [chromium-library] › tests\library\screencast.spec.ts:83:5 › start throws if screencast is already started
[187/195] [chromium-library] › tests\library\screencast.spec.ts:131:5 › start/stop twice without path creates two files in artifactsDir
[188/195] [chromium-library] › tests\library\screencast.spec.ts:94:5 › start allows restart with different options after stop
[189/195] [chromium-library] › tests\library\screencast.spec.ts:156:5 › start should work when recordVideo is set
[190/195] [chromium-library] › tests\library\screencast.spec.ts:179:5 › start should fail when another recording is in progress
[191/195] [chromium-library] › tests\library\screencast.spec.ts:186:5 › stop should not fail when no recording is in progress
[192/195] [chromium-library] › tests\library\screencast.spec.ts:194:5 › start should finish when page is closed
[193/195] [chromium-library] › tests\library\screencast.spec.ts:207:5 › should auto-save video when page closes without stop
[194/195] [chromium-library] › tests\library\screencast.spec.ts:225:5 › empty video
[195/195] [chromium-library] › tests\library\screencast.spec.ts:237:5 › start dispose stops recording
  195 passed (3.7m)
Test results - full page-close.spec.ts suite (18 tests), no regressions
[5/18] [chromium-library] › tests\library\page-close.spec.ts:65:5 › should set the page close state
[6/18] [chromium-library] › tests\library\page-close.spec.ts:71:5 › should pass page to close event
[7/18] [chromium-library] › tests\library\page-close.spec.ts:79:5 › should terminate network waiters
[8/18] [chromium-library] › tests\library\page-close.spec.ts:100:5 › should return null if parent page has been closed
[9/18] [chromium-library] › tests\library\page-close.spec.ts:92:5 › should be callable twice
[10/18] [chromium-library] › tests\library\page-close.spec.ts:110:5 › should fail with error upon disconnect
[11/18] [chromium-library] › tests\library\page-close.spec.ts:118:5 › page.close should work with window.close
[12/18] [chromium-library] › tests\library\page-close.spec.ts:124:5 › should not throw UnhandledPromiseRejection when page closes
[13/18] [chromium-library] › tests\library\page-close.spec.ts:133:5 › interrupt request.response() and request.allHeaders() on page.close
[14/18] [chromium-library] › tests\library\page-close.spec.ts:152:5 › should not treat navigations as new popups
[15/18] [chromium-library] › tests\library\page-close.spec.ts:166:5 › should not result in unhandled rejection
[16/18] [chromium-library] › tests\library\page-close.spec.ts:180:5 › should reject response.finished if page closes
[17/18] [chromium-library] › tests\library\page-close.spec.ts:200:5 › should not throw when continuing while page is closing
[18/18] [chromium-library] › tests\library\page-close.spec.ts:212:5 › should not throw when continuing after page is closed
  18 passed (3.3s)

sarang-code2 and others added 2 commits July 11, 2026 21:14
When using page.screencast.start({ path }), if the page closes before
stop() is called, the video file was never saved. The server already
stops the recording gracefully, but the client never called
artifact.saveAs().

Listen for Events.Page.Close in the client Screencast constructor and
auto-save the artifact when the page closes while a recording is active.

Fixes microsoft#41608
Addresses review feedback: microsoft#41744 (comment)

The auto-save triggered from the once(Events.Page.Close) listener was
fire-and-forget, so page.close() could resolve before the video was
actually written to disk - nothing guaranteed ordering, and a failed
save was silently swallowed with no signal at all.

Screencast now tracks the in-flight save as _pendingAutoSave, and
Page.close() awaits it in a finally block. This is safe because the
server dispatches the 'close' event (which triggers the auto-save)
before it sends the response to the close() call itself, so the
promise is always already assigned by the time page.close() reaches
its finally block.

Verified with 195 runs (13 tests x 15 repeats) of screencast.spec.ts,
all passing, plus the full page-close.spec.ts suite (47 tests) with no
regressions.
@Git-Raini

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Automatically stop the screencast video recording and save the video file when the page is closed.

2 participants