Skip to content

fix: WeakPtr crash in WebContents::DevToolsOpened() - #46389

Closed
codebytere wants to merge 1 commit into
mainfrom
speculative-fix-weak-crash
Closed

codebytere wants to merge 1 commit into
mainfrom
speculative-fix-weak-crash

Conversation

@codebytere

Copy link
Copy Markdown
Member

Description of Change

Closes #34042.

Fairly sure what's happening here is that the owner window is getting destroyed but the WeakPtr hasn't been cleared yet, so owner_window() is returning a dangling pointer, leading to a crash when SetOwnerWindow() tries to use it.

Checklist

Release Notes

Notes: none

@codebytere codebytere added semver/patch backwards-compatible bug fixes target/34-x-y PR should also be added to the "34-x-y" branch. target/35-x-y PR should also be added to the "35-x-y" branch. target/36-x-y PR should also be added to the "36-x-y" branch. labels Mar 31, 2025
@codebytere
codebytere requested review from ckerr and jkleinsc March 31, 2025 17:33
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Mar 31, 2025
if (owner_window() && !has_window)
handle->SetOwnerWindow(devtools, owner_window());
if (!has_window) {
if (const auto* window = owner_window())

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.

I don't understand how this fixes a case where owner_window() returns a dangling pointer?

The non-nullptr test on line 2159 will succeed, so both the old and new version of this function will pass the invalid pointer on to SetOwnerWindow()

@deepak1556

Copy link
Copy Markdown
Member

Fairly sure what's happening here is that the owner window is getting destroyed but the WeakPtr hasn't been cleared yet

That's not how the weakptr contract operates.

The current stack traces have limited information to confirm the idea, can we add some crash keys to

if (owner_window() && !has_window)
in DevtoolsOpened to confirm if the weakptr is indeed referring to invalid reference. The keys should capture the output of owner_window_.WasInvalidated, owner_window_.MaybeValid and if owner_window_.get() != raw_ptr_native_window. The raw_ptr_native_window can be captured when the original SetOwnerWindow call for the webcontents happens.

@codebytere

codebytere commented Apr 1, 2025 •

Copy link
Copy Markdown
Member Author

@deepak1556

in DevtoolsOpened to confirm if the weakptr is indeed referring to invalid reference.

this part I can confirm since that showed in the most recent stacktrace i saw for this crash (but now cannot find).

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Apr 1, 2025
@codebytere codebytere closed this Apr 17, 2025
@codebytere
codebytere deleted the speculative-fix-weak-crash branch April 17, 2025 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver/patch backwards-compatible bug fixes target/34-x-y PR should also be added to the "34-x-y" branch. target/35-x-y PR should also be added to the "35-x-y" branch. target/36-x-y PR should also be added to the "36-x-y" branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Crash in WebContents::DevToolsOpened -> WebContents::SetOwnerWindow

3 participants