Skip to content

fix: clear device metrics overrides when a DevTools client disconnects - #52254

Merged
codebytere merged 3 commits into
mainfrom
claude/hungry-pare-5976c6
Aug 17, 2026
Merged

codebytere merged 3 commits into
mainfrom
claude/hungry-pare-5976c6

Conversation

@codebytere

@codebytere codebytere commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Description of Change

When a CDP client sets Emulation.setDeviceMetricsOverride and disconnects without clearing it (killed process, dropped websocket), the page stays pinned at the override size: the view keeps its emulated size, and WebContentsImpl::GetSizeForMainFrame() re-applies it on every navigation. Only relaunching the app recovers. Root cause is upstream — session teardown (EmulationHandler::Disable()) skips the ClearDeviceEmulationSize() cleanup that an explicit Emulation.clearDeviceMetricsOverride performs.

This fixes it in Electron's DevToolsManagerDelegate, which content notifies for every DevTools client regardless of transport — remote-debugging websocket clients, webContents.debugger, the bundled DevTools frontend and extensions all attach, dispatch and detach through the same DevToolsSession path, and all of them hit the same missing cleanup in EmulationHandler::Disable(). HandleCommand() records which client channels hold an uncleared Emulation.setDeviceMetricsOverride, and ClientDetached() restores the WebContents' device emulation size when such a channel goes away. The bookkeeping is per channel, so a detach only undoes that channel's own override: if another still-attached client holds an override on the same WebContents, the size is left alone until that client clears it or detaches.

Verified affected: 42.5.1, 43.0.0, main, and stock Chromium — an upstream fix in EmulationHandler::Disable() is the long-term home for this. Includes regression specs for both entry points: one sets an override over a raw remote-debugging websocket, severs the connection, and asserts the page returns to the window size; the other does the same through webContents.debugger.detach().

Checklist

Release Notes

Notes: Fixed DevTools device metrics overrides persisting indefinitely when a remote debugging client disconnected without clearing them.

@codebytere codebytere added semver/patch backwards-compatible bug fixes target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. labels Jul 3, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Jul 3, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Jul 4, 2026
@MarshallOfSound

Copy link
Copy Markdown
Member

Heads up: Windows CI builds moved to new runners on Monday and the old ones have now been removed. This branch predates that, so its Windows build jobs still point at the old runners and will sit queued forever if CI runs again. Rebasing on main (or "Update branch") picks up the change, nothing else needed.

codebytere and others added 3 commits August 16, 2026 06:57
When a debugger client set Emulation.setDeviceMetricsOverride and then
went away without sending Emulation.clearDeviceMetricsOverride (killed
process, dropped websocket, or plain Target.detachFromTarget), the
emulated view size leaked: the page kept laying out at the override
size instead of the window size, WebContentsImpl::GetSizeForMainFrame()
re-applied the stale size to every new main frame on navigation, and no
later session could clear it because a fresh session's EmulationHandler
early-returns from clearDeviceMetricsOverride when its own
device_emulation_enabled_ is false. Only relaunching the app recovered.

The root cause is upstream: EmulationHandler::Disable(), which runs on
session teardown, disables the renderer-side device emulation but never
calls WebContentsImpl::ClearDeviceEmulationSize(), unlike the explicit
clearDeviceMetricsOverride path. Renderer-side emulation is correctly
torn down, so everything that leaks is browser-side and reachable from
the embedder.

Fix it without a chromium patch: track in the DevToolsManagerDelegate
which client channels have set device metrics overrides (HandleCommand
sees every command), and when such a channel detaches without having
cleared them, restore the WebContents device emulation size. Channels
that never set overrides are untouched, so an unrelated client
detaching - a webContents.debugger detach, an auto-attached OOPIF
session going away - does not disturb another session's active
emulation.

Adds a regression spec that sets an override over a raw CDP websocket,
destroys the TCP connection without a close frame, and asserts the page
returns to the window size; it fails on unpatched builds.
Also cover the webContents.debugger detach path in the debugger spec.
@codebytere
codebytere force-pushed the claude/hungry-pare-5976c6 branch from 4119608 to 35f1e06 Compare August 16, 2026 07:09
@codebytere
codebytere requested a review from deepak1556 August 16, 2026 07:53
@codebytere
codebytere merged commit 084e02b into main Aug 17, 2026
76 checks passed
@codebytere
codebytere deleted the claude/hungry-pare-5976c6 branch August 17, 2026 10:48
@release-clerk

release-clerk Bot commented Aug 17, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed DevTools device metrics overrides persisting indefinitely when a remote debugging client disconnected without clearing them.

@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "43-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot removed the target/43-x-y PR should also be added to the "43-x-y" branch. label Aug 17, 2026
@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "44-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

@VerteDinde has manually backported this PR to "44-x-y", please check out #52930

MarshallOfSound pushed a commit that referenced this pull request Aug 18, 2026
#52930)

fix: clear device metrics overrides when a DevTools client disconnects (#52254)

(cherry picked from commit 084e02b)

Co-authored-by: Shelley Vohr <[email protected]>
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. and removed in-flight/44-x-y labels Aug 18, 2026
@trop

trop Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

@VerteDinde has manually backported this PR to "43-x-y", please check out #52946

@trop

trop Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "42-x-y", please check out #53133

codebytere added a commit that referenced this pull request Aug 23, 2026
codebytere added a commit that referenced this pull request Aug 23, 2026
codebytere added a commit that referenced this pull request Aug 23, 2026
#53133)

fix: clear device metrics overrides when a DevTools client disconnects (#52254)

(cherry picked from commit 084e02b)
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels Aug 23, 2026
codebytere added a commit that referenced this pull request Aug 24, 2026
#52946)

* fix: clear device metrics overrides when a DevTools client disconnects (#52254)

(cherry picked from commit 084e02b)

* test: use the ws v7 client API on this branch

* test: use a default import for the ws client constructor

The namespace import is not constructable under tsconfig.spec.json
(nodenext implies esModuleInterop), and new-cap rejects `new ws()`.

---------

Co-authored-by: Shelley Vohr <[email protected]>
@trop trop Bot added merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants