Repository navigation
fix: keep remote api::WebContents alive while their content::WebContents lives - #52847
Merged
Merged
Conversation
…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
No-Verification-Needed: formatting-only change
deepak1556
approved these changes
Aug 17, 2026
deepak1556
left a comment
Member
There was a problem hiding this comment.
Thanks for tracking this down, LGTM
|
Release Notes Persisted
|
Contributor
|
I was unable to backport this PR to "42-x-y" cleanly; |
This was referenced Aug 17, 2026
Contributor
|
I have automatically backported this PR to "43-x-y", please check out #52903 |
Contributor
|
I have automatically backported this PR to "44-x-y", please check out #52904 |
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]>
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Change
Fixes #52158, the recurring
SIGSEGVinWebContents::DevToolsOpened()on the macOS CI shards (same bug as #29890 / #34042 / #47187).The api::WebContents for the DevTools
content::WebContentsis created up front inInspectableWebContents::ShowDevToolsand the handle is dropped. It's akRemoteWebContents so nothing pins it, and nothing references its wrapper again until the frontend has loaded andDevToolsOpened()stashes it indevtools_web_contents_. If a GC lands in between, the wrapper is collected but the C++ object is still findable viaFrom()until the deferred delete runs, andFromOrCreate()handsDevToolsOpened()an empty handle.Pin()remote api::WebContents in the constructor andUnpin()inWebContentsDestroyed(), so a remote api::WebContents lives exactly as long as itscontent::WebContents. SamePinnablemechanism background pages already use, no newv8::Global.openDevTools()anddevtools-openedand asserts exactly one DevTools api::WebContents was created and it's the one ondevToolsWebContents. Fails on the pre-fix build (expected [ 2, 3 ] to deeply equal [ 3 ]), passes with the fix.History of api::WebContents lifetime management (for the curious)
UserDataLinkWeakPtr soFrom()can find the api object from acontent::WebContents. Still nothing strong for remotes.RenderProcessReadybackstop and started eagerly creating api::WebContents for DevTools / extension hosts / mime guests. This is where "created early, referenced late" starts, CI Crash in DevToolsOpened #29890 shows up two months later.ccb16e2921) pinned background page api::WebContents inInitWithExtensionViewfor exactly this reason ("live until the underlying content::WebContents is destroyed").kRemotereturns early right above that Pin, so it was left out. That's the only Pin WebContents ever had, and there was never anUnpin().WebContents::DevToolsOpened()#46389 (2025) blamed a danglingowner_window_WeakPtr, closed. chore: debug crash on DevTools SetOwnerWindow #47243 added theDCHECKs inDevToolsOpened(theDCHECK_EQ(handle->owner_window(), nullptr)is now the line we crash on in CI builds). fix: try clearingInspectableWebContentsdelegate earlier #49406 / fix: try clearing InspectableWebContents delegate earlier #51420 / fix: crash when DevTools LoadCompleted IPC arrives after WebContents teardown #52166 all guard the inspected WebContents being freed, which is a different bug.DeleteSoonon top and widened it by another task, which lines up with Flaky crash: macOS test process SIGSEGV in WebContents::DevToolsOpened() during MenuItems toggleDevTools/reload tests #52158 showing up in June.Checklist
npm testpasses (webContents module,chrome extensions,session module,webFrameMain module,BrowserWindow module devtools,MenuItems: 558 ok / 0 failing locally)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.