question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rogerwangcommented, Jan 15, 2020

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

0reactions
rogerwangcommented, Mar 1, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Throttling in Office 365: Why it Happens and How to Avoid it
Another thing to consider is that throttling is normal and is merely a result of Office 365 ensuring that it can keep the...
Read more >
Throttling (Message Rate-Limiting)
You can choose to disable throttling for a campaign or specify a different (lower) throttle limit while creating it.
Read more >
.net - How can I throttle the amount of messages coming from ...
Sounds like your programming model would be better suited by the use of the synchronous receive calls, then you can throttle it all...
Read more >
BroadcastChannel API - A message bus for the web
The Broadcast Channel API is a simple API that makes communicating between browsing contexts easier. That is, communicating between windows/tabs ...
Read more >
Send a message to all users (like a broadcast)
None of methods is instant and each has limits. Pushing messages to channels is the fastest way to spread info (with team/channel tagging)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found