Skip to content

[Bug]: crash on browserView.webContents.destroy() call #29626

Closed

Description

@vladimiry

Preflight Checklist

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).

Testcase Gist URL

No response

Additional Information

See the attached crash stack.

Related issue: #26929.

As pointed in #26929 (comment) wrapping the destroy() call into setImmediate prevents the app from crash. So this is a workaround.

Activity

  1. zcbenz commented on Jun 15, 2021

    @zcbenz
    Contributor

    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.

  2. vladimiry commented on Jun 15, 2021

    @vladimiry
    Author

    Please consider reopening the issue @zcbenz / CC @nornagon (ref: #26929 (comment))

    • There is currently no way to release browserView-related resources other than calling browserView.webContents.destroy().
    • I believe regardless of anything there should be no hard crash.

    #26929 (comment):

    @stewartlord would you be able to file a separate bug with the crash info?

  3. added
    blocked/need-repro
    on Jun 15, 2021
  4. nornagon commented on Jun 15, 2021

    @nornagon
    Contributor

    It sounds like you're able to reproduce this fairly reliably, would it be possible to get a gist that shows the issue?

  5. vladimiry commented on Jun 15, 2021

    @vladimiry
    Author

    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.

  6. vladimiry commented on Jun 15, 2021

    @vladimiry
    Author

    @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).

    const {app, BrowserWindow, BrowserView} = require('electron')
    
    app.whenReady().then(async () => {
      const win = new BrowserWindow({width: 800, height: 600})
      await win.loadURL('data:text/html,hello there')
      for (let i = 0; i < 100; i++) {
        const view = new BrowserView();
        win.setBrowserView(view);
        view.setBounds({x: 100, y: 0, width: 700, height: 500});
        await win.webContents.loadURL(`https://www.electronjs.org?r=${Date.now()}`);
        console.log(`destroying BrowserView instance #${i}`);
        view.webContents.destroy();
        win.setBrowserView(null);
      }
    })
    
  7. abacaj commented on Jun 19, 2021

    @abacaj

    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?

    image

    Electron memory usage and process count goes up anytime you create a browser view, there should be a way to remove the browser view.

  8. vladimiry commented on Jun 19, 2021

    @vladimiry
    Author

    Based on my testing there is no way to remove it once it's created ...

    @abacaj see above message:

    • There is currently no way to release browserView-related resources other than calling browserView.webContents.destroy().

    The browserView.destroy method was available before but got removed since v11 without proper replacement.

    ... without crashing the app.

    There is a workaround which prevents the crash (wrapping the call in setImmediate):

    setImmediate(() => {
        browserView.webContents.destroy();
    });
    
  9. abacaj commented on Jun 19, 2021

    @abacaj

    Based on my testing there is no way to remove it once it's created ...

    @abacaj see above message:

    • There is currently no way to release browserView-related resources other than calling browserView.webContents.destroy().

    The browserView.destroy method was available before but got removed since v11 without proper replacement.

    ... without crashing the app.

    There is a workaround which prevents the crash (wrapping the call in setImmediate):

    setImmediate(() => {
        browserView.webContents.destroy();
    });
    

    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.

  10. vladimiry commented on Jun 19, 2021

    @vladimiry
    Author

    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.

    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).

    The only real solution is to re-use the browser view instance.

    Not the solution but a workaround and a sort of bad one since at least one process will keep running even if not required.

  11. nornagon commented on Jun 22, 2021

    @nornagon
    Contributor

    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:

    const { app, crashReporter } = require('electron')
    
    console.log(app.getPath('crashDumps'))
    crashReporter.start({ submitURL: '', uploadToServer: false })
  12. vladimiry commented on Jun 22, 2021

    @vladimiry
    Author

    @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

  13. 2 remaining items

  14. added
    status/confirmed
    has-repro-gist
    has-repro-comment
    and removed
    has-repro-gist
    on Jun 23, 2021
  15. nornagon commented on Jun 23, 2021

    @nornagon
    Contributor

    Ok, I'm able to reproduce the crash on Windows.

    Also, flipping the order of view.webContents.destroy(); and win.setBrowserView(null); seems to make the crash go away. i.e. setBrowserView(null) before webContents.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.

  16. vladimiry commented on Jun 23, 2021

    @vladimiry
    Author

    @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.

  17. nornagon commented on Jun 23, 2021

    @nornagon
    Contributor

    @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.

  18. nornagon commented on Jun 23, 2021

    @nornagon
    Contributor

    Ok, here's what I think is happening to cause this crash:

    1. The view.webContents.destroy() call runs. This causes BrowserView::WebContentsDestroyed() to run, which sets its api_web_contents_ to nullptr. (ref)
    2. win.setBrowserView(null) is called, which ends up in BaseWindow::ResetBrowserViews(). (ref).
    3. 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)
    4. 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)
    5. This results in the BrowserView pointer sticking around in the NativeWindow::browser_views() list.
    6. 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.

  19. nornagon commented on Jun 23, 2021

    @nornagon
    Contributor
  20. self-assigned this
    on Jun 23, 2021
  21. zcbenz commented on Jun 24, 2021

    @zcbenz
    Contributor

    Thanks for the investigation! Yeah storing the owner window information in the BrowserView object should be able to fix it.

  22. pushkin- commented on Jun 24, 2021

    @pushkin-

    So will this allow us to call browserView.webContents.destroy() without calling win.setBrowserView(null)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

13-x-ybug 🪲has-repro-commentplatform/linuxplatform/windowsstatus/confirmed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions