Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to disable throttling of worker / broadcast channel messages? #7090

Closed
arudnev opened this issue Jun 21, 2019 · 6 comments
Closed
Assignees

Comments

@arudnev
Copy link

arudnev commented Jun 21, 2019

It seems that after app is hidden / minimized for about 10 seconds the delivery of messages that are sent to workers via Worker.postMessage or BroadcastChannel.postMessage gets throttled and executed once per second, adding substantial delays to app logic in cases when there are multiple interactions between main and worker threads.

Is there a way to disable this throttling?

On top of that, it seems that --disable-background-timer-throttling is being ignored for setTimeout in the worker threads. So, once app is in background for 10 seconds execution of operations scheduled with setTimeout get throttled as well, even though it's not throttled on the main thread once that option is added.

Is this expected behavior for setTimeout in worker threads?

Throttling / timing is slightly different depending on release, but it's present in several latest major releases.

Here is code to replicate the issue:

main.js:

  const gui = require('nw.gui');
  const win = gui.Window.get();
  const lines = [];

  function log(msg) {
    const line = `${new Date().toISOString()}: ${msg}`;
    console.log(line);
  }
  const events = ['focus', 'blur', 'minimize', 'restore', 'maximize', 'unmaximize'];
  events.forEach(event => win.on(event, () => log(event)));
  win.on('close', (quit) => { log(`close(${quit})`); win.hide(); win.close(true); });

  const worker = new Worker('./worker.js');

  function ping() {
    const timeout = 500;
    const pingScheduled = Date.now();
    setTimeout(() => {
      const pingSent = Date.now();
      const pingDelay = pingSent - pingScheduled - timeout;
      worker.postMessage({ pingDelay, pingSent });
    }, timeout)
  }

  worker.onmessage = ({ data: { pingDelay, pingSent, pingReceived, pongDelay, pongSent } }) => {
    const pongReceived = Date.now();
    log(`ping: ${ pingDelay } + ${ pingReceived - pingSent }, pong: ${ pongDelay } + ${ pongReceived - pongSent }`);
    ping();
  };

  ping();

worker.js:

onmessage = function onMessage({ data: { pingDelay, pingSent } }) {
  const pingReceived = Date.now();
  const timeout = 500;
  setTimeout(() => {
    const pongSent = Date.now();
    const pongDelay = pongSent - pingReceived - timeout;
    postMessage({ pingDelay, pingSent, pingReceived, pongSent, pongDelay })
  }, timeout);
};

So, while app is in foreground we don't have delays in delivery of messages or above of what we request with setTimeout:

2019-06-21T21:08:46.499Z: ping: 1 + 0, pong: 0 + 0

After about 10 seconds when app is minimized or moved to another desktop we start experiencing delivery delay for message that is sent from main thread to a worker thread as well and delay for execution of operations via setTimeout:

2019-06-21T21:08:55.193Z: ping: 2 + 497, pong: 500 + 0
2019-06-21T21:08:53.193Z: ping: 3 + 165, pong: 501 + 0
2019-06-21T21:08:51.524Z: ping: 2 + 0, pong: 1 + 0
...
2019-06-21T21:08:47.506Z: ping: 4 + 0, pong: 2 + 0
2019-06-21T21:08:46.908Z: blur

Once application is restored / focused on the timing gets back to normal:

2019-06-21T21:09:39.448Z: ping: 1 + 0, pong: 0 + 1
2019-06-21T21:09:38.445Z: ping: 0 + 244, pong: 1 + 1
2019-06-21T21:09:37.941Z: focus
2019-06-21T21:09:37.192Z: ping: 1 + 498, pong: 501 + 0

It does not seem that this happens if app just blurs, but stays on current desktop in background, so one has to either minimize or move it to another desktop to reproduce.

In attached zip I included index.html, worker.js and package.json (with --disable-background-timer-throttling set in chromium-args, can be executed via yarn install && yarn start).

background-throttling.zip

@rogerwang
Copy link
Member

Will look to enable this feature. Thanks.

@rogerwang rogerwang self-assigned this Jan 15, 2020
@rogerwang
Copy link
Member

This is fixed in git and will be available in the next nightly build.

@rogerwang
Copy link
Member

This change is released with 0.43.5.

@peq42
Copy link

peq42 commented Feb 28, 2020

@rogerwang this issue is back on v0.44.3 (tested on windows 10).

@arudnev
Copy link
Author

arudnev commented Feb 29, 2020

I don't see this issue on macOS Catalina with 0.43.5 or 0.44.3, but it can be reproduced on Windows 7 and Ubuntu 16.04 with either of versions, so it seems it has not been fixed for Windows and Linux.

@rogerwang
Copy link
Member

This is fixed in git and will be available in the next nightly build. Please use --disable-raf-throttling with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants