Skip to content

"backgroundThrottling: false" no longer affects requestAnimationFrame() in background applications #9567

Closed

Description

@WesWedding
  • Electron version: 1.5.0 thru 8.0.0
  • Operating system: Mac OS Sierra 10.12.4, El Capitan 10.11.6, 11.2.3 Mac OS Big Sur

Expected behavior

Disabling background throttling should prevent the slowdowns/pausing of both animation frames and intervals/timeouts when the window is in the background.

For instance,

  mainWindow = new BrowserWindow({
   ...
    webPreferences: {
      pageVisibility: true,
      backgroundThrottling: false,
    },
    ...
  })

... should allow RAF to run without getting stopped or otherwise throttled when the window is in the background (minimized or completely obscured by other windows). Behavior specific to RAF on this issue was added in 0.37.6 (#4925)

Actual behavior

Behavior that depends on RAF calls will slow to a crawl when the window becomes obscured by other windows or is otherwise made a "background" window, behaving no differently whether backgroundThrottling is set to true or false. It reverts to normal behavior as soon as it becomes a foreground window again.

How to reproduce

Easiest to just use the test repo I put together.

git clone https://github.com/stickywes/test-electron-background-throttle.git
npm install
npm start

Workaround(s)

Use an older version of electron.

Versions below 1.5.0, such as 1.4.16, do not have this issue. The regression on this issue appears to begin with 1.5.0 onward.

Use setInterval()

westoncbb says: "setInterval() does work in the background with backgroundThrottling: false —so that may be a workaround for some."

Activity

  1. LukePulverenti commented on May 24, 2017

    @LukePulverenti

    @Stickywes do you mean to say that behavior depending on RAF will slow to a crawl even when the application is running in the foreground? Or is this issue only experienced when in the background? Thanks.

  2. WesWedding commented on May 24, 2017

    @WesWedding
    Author

    Things are great if it's in the foreground! This issue only arises when the application window becomes obscured or minimized. RAF calls resume their normal rate as soon as the application is visible again.

    I've updated the issue description for clarity.

  3. changed the title [-]"backgroundThrottling: false" no longer affects requestAnimationFrame()[/-] [+]"backgroundThrottling: false" no longer affects requestAnimationFrame() in background applications[/+] on May 24, 2017
  4. georgewoo-nsuslab commented on Jul 4, 2017

    @georgewoo-nsuslab

    Are there any progress in this issue? I'm facing exactly the same problem in
    electron version: 1.6.10
    OS: macOS Sierra 10.12.5

    Even if the backgroundThrottling option of BrowserWindow was set to false, animation stops when the application window runs in the background.

  5. westoncb commented on Jul 4, 2017

    @westoncb

    Same problem here:

    I have tried the webPreferences option 'backgroundThrottling: false', app.commandLine.appendSwitch('disable-renderer-backgrounding'),
    and win.setMinimizable(true) —in all cases requestAnimationFrame stops getting called when the window is inactive.

    electron version: 1.6.11
    OS: macOS El Capitan 10.11.6

    Edit: setInterval() does work in the background with backgroundThrottling: false —so that may be a workaround for some.

  6. neurosnap commented on Jul 25, 2017

    @neurosnap

    Any updates on this issue?

  7. StevenEBarbaro commented on Jul 26, 2017

    @StevenEBarbaro
    Contributor

    Don't know if this will help, but I tried the repro on two environments:

    Can't reproduce on Windows 8
    Can reproduce on El Capitan 10.11.6

  8. stephenou commented on Aug 15, 2017

    @stephenou

    After doing some research:

    In commit 497f5a1 (a part of Electron v1.5.0), osr_render_widget_host_view_mac.mm was changed to reflect Chromium API changes. This could be what introduced the bug. But I might be wrong here because I don't know low level Electron well enough. 😄

    This is the associated Chromium change. Search for browser_compositor_view_mac.mm.

    Additionally, the member variable disable_hidden_ set in atom_api_web_contents.cc is not found in the corresponding Chromium class or any class it inherits from, which is a little mind boggling. Where does Chromium use the disable_hidden_ property?

    If anyone can drop some knowledge on this, that'd be awesome.

  9. WesWedding commented on Aug 16, 2017

    @WesWedding
    Author

    Updating the description to call out known workarounds, since that was kind of buried in my original description. Also adding westoncb's setInterval() comment.

  10. prixe commented on Jan 30, 2018

    @prixe

    Any news of this issue? I currently still using electron 1.4.x but my user encounter some cpu/ram problem with webGL, wich are better managed in the 1.7/8.
    However my app has to still working during a workspace change or when it is minimized, i can't sacrifice this feature for mac users.

    I have tried :

    app.commandLine.appendSwitch("disable-renderer-backgrounding");
    app.commandLine.appendSwitch("disable-background-timer-throttling");

    without success.

    It's really annoying and this regression was introduced a long time ago. So if you can fix it ❤️

  11. 29 remaining items

  12. iamisti commented on Aug 16, 2021

    @iamisti

    It's still not fixed! Using latest electron. This issue is so annoying. Aslo the lack of ownership of all the issues electron has. Literally every second feature has issues...

  13. WesWedding commented on Aug 17, 2021

    @WesWedding
    Author

    One step toward a solution would be to provide an example that demonstrates the issue, and provide details like the Operating System and OS version you're using.

  14. joezappie commented on Aug 31, 2021

    @joezappie

    Also need this. I have an older app thats on Electron 9.0.3 and disabling background throttling just fine. Now I've started a new app and version 14.X doesn't work.

    We do robotics and our application streams positional data to our controller. When the app goes into the background this means our robotic kinematics stops even though in previous electron versions it would continue running. We cannot have our motion paths stop just because the window becomes hidden. Sticking to electron 9.X until this is resolved.

    I always used the app.commandLine.appendSwitch('disable-renderer-backgrounding') but the backgroundThrottling flag also does not work for me. Can this issue get reopened as its not fixed?

    OS: Windows 10 (19043)

  15. ericblade commented on Aug 31, 2021

    @ericblade

    @jrj2211 might be worth opening a new issue, this one is 4.5 years old now, but you might be well equipped to provide a proper test case that could prove out that it's broken (or at least find out what is needed to make the test case work properly in modern)

    if you do, make sure you tag back here :)

  16. joezappie commented on Aug 31, 2021

    @joezappie

    @ericblade I'm working on creating test apps and I'll start a new issue if I get a better idea of whats going on. I am perplexed though because I can't get consistent results. I created a fresh electron project with just this:

    renderer.js

    function update() {
      requestAnimationFrame(() => {
        update();
        console.log('update');
      });
    }
    update();
    

    main.js

    app.commandLine.appendSwitch('disable-renderer-backgrounding');
    app.commandLine.appendSwitch('disable-background-timer-throttling');
    app.commandLine.appendSwitch('disable-backgrounding-occluded-windows');
    
    const createWindow = () => {
      const mainWindow = new BrowserWindow({
        ...
        webPreferences: {
          backgroundThrottling: false,
        },
      });
    

    I started with Electron V14 and it was getting throttled. I've stepped back through each version of electron down to V7 at which point it stopped throttling. I changed back to V12 and V14, and its no longer throttling there either. No code changes, just did "npm install [email protected]" to change versions. I'm printing out the electron version on start to ensure its actually switching electron versions. Now I can't get it to throttle in my test example... I do not have electron installed globally so not sure whats going on there. I did also see a few times where the loop was just reduced speed (once per second) and sometimes it wouldn't start the loop until the window was visible but then was fine to minimize.

    I did find that the only thing that had an effect for RAF was backgroundThrottling. The commandline arguments did nothing for RAF as far as I could tell, but did work for fixing throttling on setInterval/setTimeout.

    I'm also trying this with a fresh electron-forge app and the same settings/code as above. Turns out I can't get it to stop throttling with any version of electron. I believe this might be an issue with electron forge and not electron though as I can get it to work with just a simple electron app (despite being inconsistent). My actual app is using electron-forge so I'm most interested in figuring out if these are separate issues.

    There's definitely more testing to be done here, but I just wanted to log my current finding as I need to move onto other stuff. Using a normal setInterval loop will work for me for the time being.

  17. ericblade commented on Sep 1, 2021

    @ericblade

    a normal setInterval loop will work for me for the time being.

    Aren't Interval timers also throttled by Chrome? or is that something that I remember from one of the many customized versions i've worked on in the past...

  18. iamisti commented on Sep 1, 2021

    @iamisti

    finally some happenings here. Looking forward the solution. It's been years and it's still not working properly. For me it's on windows 10, i have an invisible window that is getting throttled as soon as the gaming application is in focus.

  19. James-AltaML commented on Nov 30, 2021

    @James-AltaML

    https://developer.chrome.com/blog/timer-throttling-in-chrome-88/#javascript-timers

    I am not sure if this will resolve the issues with requestAnimationFrame, but for setInterval the below worked.
    A bunch of code omitted, but this worked for our project. I explicitly set throttling to false on Main and all Render processes.
    Built as Windows 10 executable

    package.json

    "devDependencies": {
        "electron": "^13.0.0",
      } 
    

    background.ts

    app.commandLine.appendSwitch("disable-background-timer-throttling");
    
    function createWindow(): BrowserWindow {
      const mainWin = new BrowserWindow({
        webPreferences: {
          backgroundThrottling: false,
        },
      });
    
        createProtocol("app");
        mainWin.webContents.setBackgroundThrottling(false);
    
      return mainWin;
    }
    

    https://www.electronjs.org/docs/latest/api/browser-window#page-visibility
    It seems that any property/function that affects visibility will enable background throttling.
    At the time of posting this, I haven't had time to fully test whether all the functions called / properties set affect throttling, so I may have went overboard.

    function createWorkerWindows(title: string): BrowserWindow {
      const workerWindow = new BrowserWindow({
        webPreferences: {
          backgroundThrottling: false,
        },
      });
    
        createProtocol("app");
        workerWindow.hide();
        workerWindow.minimize();
        workerWindow.setMovable(false);
        workerWindow.setFocusable(false);
        workerWindow.setEnabled(false);
        //the above seem to set throttling to true so call this last to set back to false
        workerWindow.webContents.setBackgroundThrottling(false);
    
      return workerWindow;
    }
    
  20. Aaqu commented on Jan 20, 2022

    @Aaqu

    I have similar problem, steps:

    • first open app app work perfectly (fiddle start)
    • close app (fiddle stop)
    • again open (fiddle start)
    • app lagging, dev tools lagging
    • minimize to bar, and open
    • app work

    Platform:
    Electron 16.0.7
    MacOs Monterey

    I write simple fiddle
    https://gist.github.com/f813a71d185cf5b0f16baf4155c9f74d

    App works when I add to BrowserWindow alwaysOnTop: true, it is not a good solution

    main.js

    const {app, BrowserWindow} = require('electron')
    
    function createWindow () {
    
      const mainWindow = new BrowserWindow({
        width: 800,
        height: 600
      })
    
      mainWindow.loadFile('index.html')
    
      mainWindow.webContents.openDevTools()
    }
    
    app.whenReady().then(() => {
      createWindow()
    
      app.on('activate', function () {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
      })
    })
    
    app.on('window-all-closed', function () {
      if (process.platform !== 'darwin') app.quit()
    })
    

    index.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
        <title>Hello World!</title>
      </head>
      <body>
    
        <select>
         <option>AAAAAA</option>  
         <option>BBBBBB</option>
         <option>CCCCCC</option>
         <option>DDDDDD</option>
         <option>EEEEEE</option>
        </select>  
      
      </body>
    </html>
    
  21. c0re100 commented on May 7, 2022

    @c0re100

    If you're facing requestAnimationFrame() isn't working in background, you may try to comment that line before compile electron.

    Just trial and error way, so I can't promise it works :')
    Well, I am not familiar electron and chromium codebase, but It works for my use case currently(at least).

  22. GrzesiekKozdroj commented on Jul 6, 2022

    @GrzesiekKozdroj

    I'd like to develop html canvas game. Multiplayer one at it too. Turn based.
    While opponents will wait for each others move, they will surely like to do a quick peek, into someone else's websites.
    This breaks animation.
    Breaks my game too!
    And this bug is closed!? Oh no, it is alive, well, kicking and breaking peoples dreams!
    Edit: Its not just OS, Firefox has it too

  23. GGAlanSmithee commented on Jun 8, 2024

    @GGAlanSmithee

    Is there any known solution to this problem? I am in the same boat as @GrzesiekKozdroj - I am developing a game using electron, and having RAF stop when the game is not in focus is more or less a show stopper for me.

    EDIT: Setting backgroundThrottling: false actually just works for me, so this seems to be fixed, at least for me (win 10, standard Electron installation)

  24. ProdigyView commented on Dec 21, 2024

    @ProdigyView

    I'm doing game as well. It does not work for me. When my canvas is in the current screen which is the main application, everything runs smoothly. If I switch to another application which put the screen in the background, the animation gets noticeably choppier. But when I click back into my app, it fixes itself. I am testing this on two separate monitors. I'm developing for Windows. This bug is not fixed.

    I've also done the following "hacks", which do not work:

    • Silent audio track that is loop
    • Have a setInterval function
    • Have the animation being processed in worker threads
    • Background throttling is false in the switches and setting it in the BrowserWindow
    • Sacrificed a Zebra to the Chromium gods

    nada works.

  25. iamisti commented on Dec 22, 2024

    @iamisti

    I'm doing game as well. It does not work for me. When my canvas is in the current screen which is the main application, everything runs smoothly. If I switch to another application which put the screen in the background, the animation gets noticeably choppier. But when I click back into my app, it fixes itself. I am testing this on two separate monitors. I'm developing for Windows. This bug is not fixed.

    I've also done the following "hacks", which do not work:

    • Silent audio track that is loop
    • Have a setInterval function
    • Have the animation being processed in worker threads
    • Background throttling is false in the switches and setting it in the BrowserWindow
    • Sacrificed a Zebra to the Chromium gods

    nada works.

    good luck brother.

    This ticket is 7 years old. I suggest you creating a new ticket. Noone will check old tockets from the team.
    Give yourself a better chance with a new ticket.

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