You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: crash on browserView.webContents.destroy() call #29626
I agree to follow the Code of Conduct that this project adheres to.
I have searched the issue tracker for a feature request that matches the one I want to file, without success.
Electron Version
13.1.2
What operating system are you using?
Other Linux
Operating System Version
kernel 5.12.8
What arch are you using?
x64
Last Known Working Electron version
No response
Expected Behavior
The app doesn't crash.
The BrowserView process gets properly destroyed.
Actual Behavior
The app crashing if browserView.webContents.destroy() called to destroy the browserView-related process (memory releasing). It doesn't crash on first call but on 5-10th browserView.create() => browserView.destroy() sequential scenario execution (in my case create/destroy calls get triggered by hotkey, so I just hit the respective hotkeys in sequence).
webContents.destroy() is really not supposed to be used unless you are familiar with Electron's source code to know when it is not going to crash, so I'm closing this as won't fix.
@nornagon the below code makes it crash. The required condition I've noticed is that you need to move the mouse over the screen. Like, start it, then allow something like 5-10 iterations to pass not interacting with the window and then move the mouse cursor to the app window and start moving the mouse cursor over it (mouse clicking is not required just moving).
Crashes for me when calling destroy. electron ./src/electron/start.js exited with code 3221225477
If I use: window.removeBrowserView(window.getBrowserView());
The view is removed from the main window but the extra electron process remains open...
This feature is completely broken as far as I can tell, if you use this you are bound to cause issues in your app as far as memory usage goes unless you are able to guarantee that you will reuse the same browser view.
What is the correct way to create a browser view and then destroy / release the extra process created?
Based on my testing there is no way to remove it once it's created without crashing the app.
@zcbenz it's not clear why you closed this, it's almost certainly a bug unless we are expected to always reuse the same BrowserView?
Electron memory usage and process count goes up anytime you create a browser view, there should be a way to remove the browser view.
@nornagon just tested #29626 (comment) on both 14.0.0-beta.8 & 13.1.3 versions running linux & windows. It's reproducible for both versions on both linux and windows systems. But it crashes faster on 14.0.0-beta.8 (faster starting from mouse moving over the app screen, so it takes less destroying iterations). So I think it's better to use 14.0.0-beta.8 for testing.
Also, flipping the order of view.webContents.destroy(); and win.setBrowserView(null); seems to make the crash go away. i.e. setBrowserView(null)beforewebContents.destroy().
I think the crash is because destroying the WebContents is causing the browserview to be destroyed, but isn't properly cleaning up the details of it in the browser window.
@nornagon btw, the browserView had destroy method before, can it get back so there is a single way to do proper destroying? A need to call webContents.destroy looks awkward since we don't explicitly create webContents but browserView.
@vladimiry#26929 broadly covers this request. Once that close method is available, we should add browserView.close() that just calls browserView.webContents.close() under the hood. There's no need for BrowserView itself to be destroyed explicitly, since the only persistent resource related to a BrowserView is the WebContents.
Ok, here's what I think is happening to cause this crash:
The view.webContents.destroy() call runs. This causes BrowserView::WebContentsDestroyed() to run, which sets its api_web_contents_ to nullptr. (ref)
win.setBrowserView(null) is called, which ends up in BaseWindow::ResetBrowserViews(). (ref).
ResetBrowserView checks if (browser_view->web_contents()), and uses browser_view->web_contents()->owner_window() to check that the BrowserView is still in the same window that it was added to, and hasn't been reparented. (ref)
HOWEVER, by this point, browser_view->web_contents() is nullptr! So it can't check the owner_window of the WebContents, so it does not call BaseWindow::RemoveBrowserView on the browser view. It does, however, remove the v8::Global that keeps the BrowserView object alive, freeing it to be garbage-collected by V8. (ref)
This results in the BrowserView pointer sticking around in the NativeWindow::browser_views() list.
Later, when a mouse event is delivered, NativeWindowViews::ShouldDescendIntoChildForEventHandling iterates the browser_views() list to check draggable regions. This is a Use-After-Free, because by this point the BrowserView could have been deleted by V8's GC. (ref)
I think the solution here has to involve BrowserView knowing which window it's owned by, rather than using WebContents to indirect that knowledge.
Activity
zcbenz commentedon Jun 15, 2021
webContents.destroy()is really not supposed to be used unless you are familiar with Electron's source code to know when it is not going to crash, so I'm closing this as won't fix.vladimiry commentedon Jun 15, 2021
Please consider reopening the issue @zcbenz / CC @nornagon (ref: #26929 (comment))
browserView-related resources other than callingbrowserView.webContents.destroy().#26929 (comment):
nornagon commentedon Jun 15, 2021
It sounds like you're able to reproduce this fairly reliably, would it be possible to get a gist that shows the issue?
vladimiry commentedon Jun 15, 2021
I can't guarantee that I will be able to provide the minimal reproducible code sample since so far I was able to reproduce the crash using https://github.com/vladimiry/ElectronMail which is a relatively heavy thing but I will try and share result then.
vladimiry commentedon Jun 15, 2021
@nornagon the below code makes it crash. The required condition I've noticed is that you need to move the mouse over the screen. Like, start it, then allow something like 5-10 iterations to pass not interacting with the window and then move the mouse cursor to the app window and start moving the mouse cursor over it (mouse clicking is not required just moving).
abacaj commentedon Jun 19, 2021
Crashes for me when calling destroy.
electron ./src/electron/start.js exited with code 3221225477If I use:
window.removeBrowserView(window.getBrowserView());The view is removed from the main window but the extra electron process remains open...
This feature is completely broken as far as I can tell, if you use this you are bound to cause issues in your app as far as memory usage goes unless you are able to guarantee that you will reuse the same browser view.
What is the correct way to create a browser view and then destroy / release the extra process created?
Based on my testing there is no way to remove it once it's created without crashing the app.
@zcbenz it's not clear why you closed this, it's almost certainly a bug unless we are expected to always reuse the same BrowserView?
Electron memory usage and process count goes up anytime you create a browser view, there should be a way to remove the browser view.
vladimiry commentedon Jun 19, 2021
@abacaj see above message:
The
browserView.destroymethod was available before but got removed since v11 without proper replacement.There is a workaround which prevents the crash (wrapping the call in
setImmediate):abacaj commentedon Jun 19, 2021
I don't see how that would help, I did try and it doesn't work for me. App crashes with same exit code issue. Electron version 12.0.11
The only real solution is to re-use the browser view instance.
vladimiry commentedon Jun 19, 2021
I think it might help then if you provide the crash dump similar to what I attached in original report (I won't be ready to guide how to do that).
Not the solution but a workaround and a sort of bad one since at least one process will keep running even if not required.
nornagon commentedon Jun 22, 2021
hm, @vladimiry I haven't been able to reproduce the crash locally using 13.1.2 on macOS, so perhaps this is Linux-specific..?
A bit of a stretch, but a minidump might be helpful additional context over and above the raw stack trace:
vladimiry commentedon Jun 22, 2021
@nornagon just tested #29626 (comment) on both 14.0.0-beta.8 & 13.1.3 versions running linux & windows. It's reproducible for both versions on both linux and windows systems. But it crashes faster on 14.0.0-beta.8 (faster starting from mouse moving over the app screen, so it takes less destroying iterations). So I think it's better to use 14.0.0-beta.8 for testing.
Attaching dump + its stack of running #29626 (comment) on window system:
d91eea6b-69f2-49ac-bfc7-004555287d91.dmp.txt
d91eea6b-69f2-49ac-bfc7-004555287d91.dmp.stack.txt
2 remaining items
nornagon commentedon Jun 23, 2021
Ok, I'm able to reproduce the crash on Windows.
Also, flipping the order of
view.webContents.destroy();andwin.setBrowserView(null);seems to make the crash go away. i.e.setBrowserView(null)beforewebContents.destroy().I think the crash is because destroying the WebContents is causing the browserview to be destroyed, but isn't properly cleaning up the details of it in the browser window.
vladimiry commentedon Jun 23, 2021
@nornagon btw, the
browserViewhaddestroymethod before, can it get back so there is a single way to do proper destroying? A need to callwebContents.destroylooks awkward since we don't explicitly createwebContentsbutbrowserView.nornagon commentedon Jun 23, 2021
@vladimiry #26929 broadly covers this request. Once that close method is available, we should add
browserView.close()that just callsbrowserView.webContents.close()under the hood. There's no need for BrowserView itself to be destroyed explicitly, since the only persistent resource related to a BrowserView is the WebContents.nornagon commentedon Jun 23, 2021
Ok, here's what I think is happening to cause this crash:
view.webContents.destroy()call runs. This causesBrowserView::WebContentsDestroyed()to run, which sets itsapi_web_contents_tonullptr. (ref)win.setBrowserView(null)is called, which ends up inBaseWindow::ResetBrowserViews(). (ref).ResetBrowserViewchecksif (browser_view->web_contents()), and usesbrowser_view->web_contents()->owner_window()to check that the BrowserView is still in the same window that it was added to, and hasn't been reparented. (ref)browser_view->web_contents()is nullptr! So it can't check the owner_window of the WebContents, so it does not callBaseWindow::RemoveBrowserViewon the browser view. It does, however, remove the v8::Global that keeps the BrowserView object alive, freeing it to be garbage-collected by V8. (ref)NativeWindow::browser_views()list.NativeWindowViews::ShouldDescendIntoChildForEventHandlingiterates thebrowser_views()list to check draggable regions. This is a Use-After-Free, because by this point the BrowserView could have been deleted by V8's GC. (ref)I think the solution here has to involve BrowserView knowing which window it's owned by, rather than using WebContents to indirect that knowledge.
nornagon commentedon Jun 23, 2021
cc @zcbenz
zcbenz commentedon Jun 24, 2021
Thanks for the investigation! Yeah storing the owner window information in the BrowserView object should be able to fix it.
pushkin- commentedon Jun 24, 2021
So will this allow us to call
browserView.webContents.destroy()without callingwin.setBrowserView(null)?