Is it possible to disable throttling of worker / broadcast channel messages?
See original GitHub issueIt 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
).
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
This is fixed in git and will be available in the next nightly build.
This is fixed in git and will be available in the next nightly build. Please use
--disable-raf-throttling
with the fix.