Skip to content

fix: keep remote api::WebContents alive while their content::WebContents lives - #52847

Merged
MarshallOfSound merged 2 commits into
mainfrom
sam/fix-devtools-api-web-contents-gc
Aug 17, 2026
Merged

MarshallOfSound merged 2 commits into
mainfrom
sam/fix-devtools-api-web-contents-gc

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Aug 16, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Fixes #52158, the recurring SIGSEGV in WebContents::DevToolsOpened() on the macOS CI shards (same bug as #29890 / #34042 / #47187).

The api::WebContents for the DevTools content::WebContents is created up front in InspectableWebContents::ShowDevTools and the handle is dropped. It's a kRemote WebContents so nothing pins it, and nothing references its wrapper again until the frontend has loaded and DevToolsOpened() stashes it in devtools_web_contents_. If a GC lands in between, the wrapper is collected but the C++ object is still findable via From() until the deferred delete runs, and FromOrCreate() hands DevToolsOpened() an empty handle.

openDevTools()                                                devtools frontend "loadCompleted"
  |                                                                          |
  ShowDevTools: FromOrCreate(devtools wc)  ->  api::WebContents #2           LoadCompleted -> DevToolsOpened
  |             (handle dropped, wrapper only weakly held)                    |  FromOrCreate(devtools wc)
  |                                                                          |
  |            GC: wrapper of #2 collected                                    |
  |            FirstWeakCallback: dead_ = true, wrapper_.Reset()              |
  |            (delete is posted, From() still finds #2)                     |
  |                  |                                                        |
  |                  +--- delete task runs before loadCompleted  ------------>|  From() finds nothing, creates #3
  |                  |                                                        |  (web-contents-created fires twice,
  |                  |                                                        |   devToolsWebContents.id != #2)
  |                  |                                                        |
  |                  +--- loadCompleted arrives before the delete  ---------->|  From() returns zombie #2
                                                                             |  GetWrapper() empty -> handle.get() == nullptr
                                                                             |  handle->owner_window() => SEGV_ACCERR
                                                                             |  (0x1e0 on main, 0x1d0 on 42-x-y = offsetof(owner_window_))
  • Fix: Pin() remote api::WebContents in the constructor and Unpin() in WebContentsDestroyed(), so a remote api::WebContents lives exactly as long as its content::WebContents. Same Pinnable mechanism background pages already use, no new v8::Global.
  • Regression spec forces a major GC between openDevTools() and devtools-opened and asserts exactly one DevTools api::WebContents was created and it's the one on devToolsWebContents. Fails on the pre-fix build (expected [ 2, 3 ] to deeply equal [ 3 ]), passes with the fix.
  • fix: crash when DevTools LoadCompleted IPC arrives after WebContents teardown #52166 wasn't the fix for this, it protects the inspected api::WebContents. Here that one is alive and the DevTools one is the zombie; the crash reproduced on 42-x-y with fix: crash when DevTools LoadCompleted IPC arrives after WebContents teardown #52511 present.
History of api::WebContents lifetime management (for the curious)

Checklist

  • I have built and tested this change
  • I have filled out the PR description
  • npm test passes (webContents module, chrome extensions, session module, webFrameMain module, BrowserWindow module devtools, MenuItems: 558 ok / 0 failing locally)
  • tests are changed or added

Release Notes

Notes: Fixed a rare crash in the main process when DevTools were opened and a garbage collection ran before the DevTools frontend finished loading.

…nts lives

The api::WebContents wrapping a DevTools WebContents is created up front in
InspectableWebContents::ShowDevTools but only referenced again once the
frontend has loaded. It is a kRemote WebContents, so nothing pinned it and a
GC in between collected its wrapper; DevToolsOpened() then either got an
empty handle back from FromOrCreate() and crashed dereferencing it, or
created a second api::WebContents for the same DevTools WebContents.

Pin remote api::WebContents in the constructor and unpin them in
WebContentsDestroyed(), the same mechanism background pages already use.

Fixes #52158
@MarshallOfSound MarshallOfSound added semver/patch backwards-compatible bug fixes target/42-x-y PR should also be added to the "42-x-y" branch. 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 Aug 16, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 16, 2026
No-Verification-Needed: formatting-only change
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 17, 2026

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down, LGTM

@MarshallOfSound
MarshallOfSound merged commit 881d822 into main Aug 17, 2026
129 of 131 checks passed
@release-clerk

release-clerk Bot commented Aug 17, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed a rare crash in the main process when DevTools were opened and a garbage collection ran before the DevTools frontend finished loading.

@MarshallOfSound
MarshallOfSound deleted the sam/fix-devtools-api-web-contents-gc branch August 17, 2026 11:43
@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

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

@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "43-x-y", please check out #52903

@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "44-x-y", please check out #52904

@trop trop Bot added in-flight/44-x-y and removed 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 Aug 17, 2026
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/44-x-y in-flight/43-x-y labels Aug 17, 2026
snackzone added a commit to snackzone/electron that referenced this pull request Aug 18, 2026
One conflict, in WebContents::WebContentsDestroyed(): electron#52847 added Unpin()
at the top of the function, where this branch had added the release of the
process-wide caret browsing count. Both sides want to be there, so both are
kept.

The caret release goes first, so the count is dropped before Unpin() makes
the wrapper collectible, and its comment is trimmed to avoid repeating the
"underlying content::WebContents is gone" preamble now sitting under it.

electron#52847 pins Type::kRemote wrappers for the life of their content::WebContents
and unpins them here. That makes the release in WebContentsDestroyed() more
load-bearing than when it was written, not less: a remote wrapper is now
pinned for its whole life, so its destructor cannot run until a GC after
this Unpin(), and the count would otherwise be held for that whole window.

Co-Authored-By: Claude Opus 5 <[email protected]>
@trop

trop Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

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

MarshallOfSound added a commit that referenced this pull request Aug 19, 2026
…nts lives (#52994)

fix: keep remote api::WebContents alive while their content::WebContents lives (#52847)

* fix: keep remote api::WebContents alive while their content::WebContents lives

The api::WebContents wrapping a DevTools WebContents is created up front in
InspectableWebContents::ShowDevTools but only referenced again once the
frontend has loaded. It is a kRemote WebContents, so nothing pinned it and a
GC in between collected its wrapper; DevToolsOpened() then either got an
empty handle back from FromOrCreate() and crashed dereferencing it, or
created a second api::WebContents for the same DevTools WebContents.

Pin remote api::WebContents in the constructor and unpin them in
WebContentsDestroyed(), the same mechanism background pages already use.

Fixes #52158

* chore: format spec

No-Verification-Needed: formatting-only change
(cherry picked from commit 881d822)

Co-authored-by: Samuel Attard <[email protected]>
@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 19, 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.

Flaky crash: macOS test process SIGSEGV in WebContents::DevToolsOpened() during MenuItems toggleDevTools/reload tests

2 participants