Skip to content

Memory leak when passing IPC events over contextBridge #27039

Closed

Description

@PDStig

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 11.1.0 and 9.3.5
  • Operating System:
    • Windows 10 2004
  • Last Known Working Electron version:
    • NA

Expected Behavior

use of webContents.send + ipcRenderer.on cleans up memory after use

Actual Behavior

In a real-world case, I have a renderer for the UI and a separate browserWindow for an audio process. The audio process monitors audio devices and reports the current volume of all devices via ipcRenderer.send (through preload contextBridge to the main process). The main process then sends this volume data over to the main UI renderer via webContents.send whereas the UI renderer listens via ipcRenderer.on (via preload contextBridge). This happens about once every 100 milliseconds. Memory use of the electron app, especially the renderer, slowly climbs to theoretical Infinitum. The starting memory is about 350MB. Within about a day, it's up to 1,500MB.

It does not matter what kind of content is passed through as arguments, nor its size (garbage collector seems to work on arguments). What matters is how often / frequently webContents.send is called and if there is an ipcRenderer.on listener for it (memory leak still exists even if there is nothing in the listener function so long as the listener exists).

To Reproduce

Electron fiddle with a very simple example calling webContents.send every millisecond (to make the test quick... but the issue exists if you do every 100 milliseconds too but is much slower) and sending a Math.random() random number to the renderer. Memory use will increase over the course of a few minutes. I tested with a 10,000-item map as an argument as well, which made memory spike very quickly and get garbage-collected repeatedly, resulting in an average memory growth of about the same as Math.random(). This leads me to believe the problem is not the data being sent but the IPC execution itself.

https://gist.github.com/37cde4adc11b3aa3f435e5d2698d9e05

Additional Information

Additional info: Using handle/invoke seems to bypass this memory leak. However this is not ideal because renderer UI has to query main every 100 milliseconds for audio volume, and main has to locally store volume information (via another invoke / handle between main and the audio process). This creates additional VU meter lag.

Activity

  1. added
    has-repro-gist
    on Dec 16, 2020
  2. nornagon commented on Dec 16, 2020

    @nornagon
    Contributor

    Hm, I can't reproduce this on macOS. Memory usage is hovering steadily around 130–140mb.

    Slightly modified gist including memory logging: https://gist.github.com/a2a70e7f6ff7cd46e0a09485d838ea44

  3. PDStig commented on Dec 17, 2020

    @PDStig
    Author

    @nornagon I used the fiddle you have. process.memoryUsage is not catching the leak as it seems to be happening in the renderer process.

    image

  4. nornagon commented on Dec 17, 2020

    @nornagon
    Contributor

    @Lovinity ah, thanks for the clarification. I see the leak now. It seems to be related to sending the IPC event over contextBridge.

    Here's an updated gist with a commented-out modification in preload.js which, if swapped in, results in no leakage. https://gist.github.com/1c4bffaf558fbec5bf7b6de98009968b

    cc @MarshallOfSound as this is contextBridge-related.

  5. changed the title [-]webContents.send and ipcRenderer.on potential memory leak[/-] [+]Memory leak when passing IPC events over contextBridge[/+] on Dec 17, 2020
  6. nornagon commented on Dec 17, 2020

    @nornagon
    Contributor

    (As a side note, btw, exposing ipcRenderer.on directly over the context bridge is a security issue and somewhat defeats the purpose of context isolation. I'd recommend not doing that in any production app.)

  7. PDStig commented on Dec 17, 2020

    @PDStig
    Author

    @nornagon What would you recommend a good alternative be for utilizing ipcRenderer on contextBridge? The only "direct" reference I have is for ipcRenderer.on (but it is still a function calling ipcRenderer.on, not a direct exposure of ipcRenderer.on). For all my "send"s, I use properties on contextBridge that call specific channels on ipcRenderer.send.

  8. nornagon commented on Dec 17, 2020

    @nornagon
    Contributor

    @Lovinity expose specific methods that your app needs, instead of allowing the app to listen to any IPC message.

  9. nornagon commented on Feb 4, 2021

    @nornagon
    Contributor

    I've updated the gist at https://gist.github.com/1c4bffaf558fbec5bf7b6de98009968b to simplify it somewhat. It now doesn't need any interaction with IPC or events. This is enough to trigger the leak:

    // preload.js
    let g_fn = null
    
    contextBridge.exposeInMainWorld("test", {
      on: (fn) => g_fn = fn,
    });
    
    setInterval(() => {
      if (g_fn)
        g_fn(function() {})
    })
    
    // renderer.js
    window.test.on(() => {});

    Just sending a function back over the bridge results in leaks.

  10. added a commit that references this issue on Feb 4, 2021
    1443ebe
  11. added a commit that references this issue on Feb 5, 2021
    b6a91ef
  12. added 3 commits that reference this issue on Feb 5, 2021
    aca3323
    45ddd2b
    ea1fb5b
  13. added 3 commits that reference this issue on Feb 5, 2021
    7c67c1d
    a2f7772
    0933c67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    11-x-y9-x-ybug 🪲component/context-bridgecomponent/ipchas-repro-gistperformance 🏁

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions