New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: crash in BrowserWindow destructor after win.webContents.destroy() #18686
Conversation
I believe this can also fix #16580. |
b1e9fd1
to
ef8be62
Compare
@zcbenz can you please review again? I think it's cleaner to make |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the relationship of a BrowserWindow
and WebContents
, the reason for depending on a bare pointer was that a window will always outlive the webContents it embeds and ensure its destruction. The bug this PR fixes explicitly calls destroy
which is not the right shutdown sequence for a webContents
, its more of a force destroy scenario. The destroy
api was never intended for public use because it can mess up destruction sequence. While I am neutral with the changes in this PR, a little concerned about the relationship change.
/cc @zcbenz
@@ -67,7 +67,7 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate, | |||
} | |||
|
|||
web_contents_.Reset(isolate, web_contents.ToV8()); | |||
api_web_contents_ = web_contents.get(); | |||
api_web_contents_ = web_contents->GetWeakPtr(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If its a weak ptr, should null check in other places of access too.
@deepak1556 Similar issue seems to manifest not only when the |
weakptr has this implicit notion, that objects accessing it doesn't care about the lifetime and can handle them safely. Most useful when posting tasks. The raw ptr here , was to explicitly state that @zcbenz has worked recently on refactoring the destruction sequence, would like his opinion before merging. |
The lifetime management of WebContents is a mess, even after I tried to clean it up. For #16580, I encountered it occasionally when running small tests, I believe we have code in Electron that incorrectly destroys WebContents before destroying BrowserWindow, but I could not find it. I would say this PR is a hack rather than a proper fix, but before we find one, I think this can be accepted as a special hack to fix crash when destroying window. We should probably add a FIXME comment in |
@miniak can you update following cheng's comment, I can approve it after that. Thanks! |
ef8be62
to
6b5268c
Compare
@deepak1556 FIXME comment added |
A maintainer has manually backported this PR to "5-0-x", please check out #18794 |
A maintainer has manually backported this PR to "4-2-x", please check out #18795 |
A maintainer has manually backported this PR to "3-1-x", please check out #18796 |
@@ -93,7 +93,9 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate, | |||
} | |||
|
|||
BrowserWindow::~BrowserWindow() { | |||
api_web_contents_->RemoveObserver(this); | |||
// FIXME This is a hack rather than a proper fix preventing shutdown crashes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the long-term plan for fixing this?
Description of Change
Fixes #18685
Make
api_web_contents_
a weak pointer to avoid accessing a destroyedapi::WebContents
instance./cc @zcbenz
Checklist
npm test
passesRelease Notes
Notes: Fixed crash in BrowserWindow destructor after
win.webContents.destroy()
.