Skip to content

API: Documented way to close a WebContents #26929

Closed

Description

@nornagon

There should be a documented way to close a WebContents and allow it to be garbage collected.

I'd prefer not to document the existing webContents.destroy() method if possible as it's quite aggressive and makes the underlying WebContents object unusable after being called. Ideally, I'd like to have a webContents.close() method that returns the object to the state that it was in prior to loadURL() being called, and marks it as available for GC once references are dropped.

i.e. this should work:

const bv = new BrowserView
bv.webContents.loadURL("https://example.com")
// ... later ...
bv.webContents.close()
// If the reference to `bv` were dropped here, both `bv` and the webContents would be available for collection

// ... but if the reference is retained, the WebContents should be re-usable at a later date:
bv.webContents.loadURL("https://example.com")

Ref #23578.

Activity

  1. kevinlinv commented on Jan 4, 2021

    @kevinlinv
    Contributor

    Heya @nornagon @ckerr, Happy new year 🎉

    I noticed that this issue has been labeled as an enchancement, is there any way we can label it with something that convey a bit more urgency? e.g. a bug/regression perhaps.

    Reasoning as partially quoted from the #23578 (comment) by @yohan1234 in PR discussion which removes BrowserView::destroy.

    [...] The problem is:

    • BrowserView.destroy was removed
    • webContents does not have a public API destroy function

    [...]

    So a previously working browser view (in electron v10 and below) now cause a memory leak (in electron v11 and above) which makes it unusable. 1 work around proposed in the discussion was to use an undocumented webContents.destroy() which is not ideal so would love to avoid it if possible.

  2. pushkin- commented on Jan 5, 2021

    @pushkin-
  3. kevinlinv commented on Jan 5, 2021

    @kevinlinv
    Contributor

    👋 @pushkin- thanks for linking, OOC, are you currently on electron v11 in your app?
    if so, have you been using webContents.destroy?
    I am interested in whether the method is safe enough to use without causing any crashes 🤔

  4. pushkin- commented on Jan 5, 2021

    @pushkin-

    @kevinlinv Yes I'm on Electron 11. I encountered a crash which was because I wasn't detaching the BrowserView from the BrowserWindow before destroying its WebContents, but now that I'm doing that, I haven't seen any crashes.

  5. kevinlinv commented on Jan 5, 2021

    @kevinlinv
    Contributor

    Nice, thanks for the info, I might consider using webContents.destroy and update to electron v11 given the need to support Apple ARM 👌

  6. self-assigned this
    on Jan 5, 2021
  7. kevinlinv commented on Jan 15, 2021

    @kevinlinv
    Contributor

    A friendly 🏓 sorry for the noise. Would appreciate if this issue is looked at by someone who knows the codebase 👍
    It's rather unfortunate for browser view to leak in latest version of electron by default.

  8. stewartlord commented on Jan 20, 2021

    @stewartlord
    Contributor

    +1 for this issue. The work-around (calling destroy() on webContents) addressed the leak for me, but I did experience a hard crash that required wrapping the destroy call in setImmediate to avoid.

  9. nornagon commented on Feb 2, 2021

    @nornagon
    ContributorAuthor

    a previously working browser view (in electron v10 and below) now cause a memory leak (in electron v11 and above) which makes it unusable.

    Just so I'm clear on what the issue is here, the app were previously calling browserview.destroy(), then that function was removed—so the app was changed to not call destroy(), and now leaks?

    I think this should be straightforwardly fixable by calling browserview.webContents.destroy() where there was previously a call to browserview.destroy(). I'm aware that the WebContents.destroy() method is not documented, but in this case I think it's OK to use. Once there exists a documented way to shut down a WebContents, the destroy() call can be replaced with that.

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

  10. kevinlinv commented on Feb 3, 2021

    @kevinlinv
    Contributor

    Hey @nornagon thanks for the reply.

    Just so I'm clear on what the issue is here, the app were previously calling browserview.destroy(), then that function was removed—so the app was changed to not call destroy(), and now leaks?

    Yes, it's exactly as you've pointed out.

    I think this should be straightforwardly fixable by calling browserview.webContents.destroy() where there was previously a call to browserview.destroy(). I'm aware that the WebContents.destroy() method is not documented, but in this case I think it's OK to use. Once there exists a documented way to shut down a WebContents, the destroy() call can be replaced with that.

    True, from my own use case, I'd prefer to not use undocumented api, but not too fussed if I really have to and will do so to allow upgrading to electron@11


    It is more into the point of view of new BrowserView users in latest electron that would make this issue slightly unfortunate, as they would immediately run into a memory leak and may not be aware of it before they stumble into this issue somehow.

  11. vladimiry commented on Jun 10, 2021

    @vladimiry

    +1 for this issue. The work-around (calling destroy() on webContents) addressed the leak for me, but I did experience a hard crash that required wrapping the destroy call in setImmediate to avoid.

    @stewartlord I've just placed the respective issue with attached core dump.

  12. stewartlord commented on Jun 10, 2021

    @stewartlord
    Contributor

    @vladimiry Thank you 🙏 ..and @nornagon looks like I failed to deliver repro steps - very sorry about that!

  13. vladimiry commented on Jun 15, 2021

    @vladimiry
  14. removed their assignment
    on Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions