KenD
KenD

Reputation: 5318

SignalR and/or timer issues since Chrome 88

We have an ASP.Net WebForms application that uses SignalR (v2.4.1) to do some bi-directional communications between server and client. It's worked fine for years: connections are stable, hundreds of users use it, etc.

However, we've started to get sporadic reports of connection problems from across our client base, all reporting the same thing: if the browser (Chrome) session goes idle for more than 5 minutes, the connection drops in the background. All timers in the page stop being run regularly, which (amongst other things) stops "keepalives" stop being sent, and eventually the connection fails with the client-side error:

The client has been inactive since <date> and it has exceeded the inactivity timeout of 50000 ms. Stopping the connection.

Standard procedure after this would be to automatically restart the connection, but this doesn't do anything. If/when the user reactivates the page (e.g. by switching to the tab), everything starts to spring back into life, albeit with a closed SignalR connection.

After much investigation, it seems that we're being impacted by this change introduced in Chrome v88, where timers (setTimeouts) are severely restricted if

The 5 minutes/30 seconds condition fits with the reports we're getting. However, we're running pretty basic Javascript on our page: there are only two uses of setTimeout in our own code, neither of which could ever "chain" (recurse) onto themselves. We also cannot replicate the issue: it's happened to us in testing, but we can't make it happen reliably. Disabling this feature via chrome://flags/#intensive-wake-up-throttling seems to mitigate the issue - but of course, we can't make this a requirement to use our site.

The only other Javascript running on the site is jquery.signalR-2.4.1.js, and from the SignalR source, there are lots of setTimeouts in there. Could SignalR be impacted by this change in Chrome; perhaps when it tries to silently reconnect after a temporary network issue or some other unpredictable event?

If not, is there any way, in any browser or IDE, to track which timers have been launched (and, more importantly, "chained"), so we can see what could be triggering this restriction?

Upvotes: 16

Views: 8629

Answers (4)

KenD
KenD

Reputation: 5318

Microsoft have released SignalR 2.4.2, which should address the issue natively and avoid the need for any manual workarounds.

Nuget package available here, and the list of fixed issues is here

Upvotes: 4

Renjith Alexander
Renjith Alexander

Reputation: 41

As a workaround, javascript library that does the ping can be modified, to slightly change the way that it uses the timers. One of the conditions for intensive throttling is that the setTimeout()/setInterval() chain count is 5+. This can be avoided for recurring calls, by using a web worker. The main thread can post a dummy message to the web worker, which does nothing other than posting a dummy message back to the main thread. The subsequent setTimeout() call can be made on the message event from the web worker.

i.e.,

  1. main_thread_ping_function :- doPing() -> post_CallMeBack_ToWebWorker()
  2. web_worker :- onmessage -> post_CallingYouBack_ToMainThread()
  3. main_thread :- web_worker.onmessage -> setTimeout(main_thread_ping_function, timeoutValue)

Since the setTimeout() is called on a message from web worker, rather than from the setTimout() execution flow, the chain length remains one, and thus no intensive throttling would be done by chrome 88+.

Note that, chained setTimeout() calls in a web worker are not throttled by chrome at the moment, and thus defining the timer functionality inside a web worker, and acting on the messages(to perform ping) from web worker, too solves the problem. However, if chrome developers decide to throttle the timers in web workers too, in the future, it gets broken again.

A utility(similar to java scheduled executor) which allows scheduling of callbacks using web workers, to avoid throttling, by context switching:

class NonThrottledScheduledExecutor {


  constructor(callbackFn, initialDelay, delay) {
    this.running = false;
    this.callback = callbackFn;
    this.initialDelay = initialDelay;
    this.delay = delay;
  };

  start() {
    if (this.running) {
      return;
    }
    this.running = true;
    // Code in worker.
    let workerFunction = "onmessage = function(e) { postMessage('fireTimer'); }";
    this.worker = new Worker(URL.createObjectURL(new Blob([workerFunction], {
      type: 'text/javascript'
    })));
    // On a message from worker, schedule the next round.
    this.worker.onmessage = (e) => setTimeout(this.fireTimerNow.bind(this), this.delay);
    // Start the first round.
    setTimeout(this.fireTimerNow.bind(this), this.initialDelay);

  };

  fireTimerNow() {
    if (this.running) {
      this.callback();
      // dummy message to be posted to web worker.
      this.worker.postMessage('callBackNow');
    }
  };

  stop() {
    if (this.running) {
      this.running = false;
      this.worker.terminate();
      this.worker = undefined;
    }
  };

};
<button onclick="startExecutor()">Start Executor</button>
<button onclick="stopExecutor()">Stop Executor</button>
<div id="op"></div>
<script>
  var executor;
  
  function startExecutor() {
    if (typeof(executor) == 'undefined') {
      // Schedules execution of 'doThis' function every 2seconds, after an intial delay of 1 sec
      executor = new NonThrottledScheduledExecutor(doThis, 1000, 2000);
      executor.start();
      console.log("Started scheduled executor");
    }
  }

  function stopExecutor() {
    if (typeof(executor) != 'undefined') {
      executor.stop();
      executor = undefined;
      document.getElementById("op").innerHTML = "Executor stopped at " + l;
    }
  }

  var l = 0;

  function doThis() {
    l = l + 1;
    document.getElementById("op").innerHTML = "Executor running... I will run even when the my window is hidden.. counter: " + l;
  }
</script>

Upvotes: 4

Chonsu
Chonsu

Reputation: 185

We're as well facing issues with our signalR (WebSockets as transport). We're not able to reproduce it in our lab. The HAR files of our customer and extended logging provided us only the information that the client "consuming only after following interesting groups" is not sending pings within the default 30 seconds needed to keep the connection. Therefore the server closes the connection. We added logs in the signalR client library and only saw the ping timer not being hit on time. No error, no nothing. (Client is JavaScript and the issue occurred on customer site in chrome 87 (throttling was implemented there already for half of the chrome users - https://support.google.com/chrome/a/answer/7679408#87))

And the world is slowly getting aware of "an issue": https://github.com/SignalR/SignalR/issues/4536

Our quick help for our customers will be to create an ET with a manual broadcast ping-pong mechanism from the server site and each client will have to answer. Avoiding being dependent on the JavaScript ping in the signalR library until a "better" solution or fix is provided.

Upvotes: 9

Mirronelli
Mirronelli

Reputation: 780

I know that it does not solve the problem altogether with chrome, however, the new edge that uses chromium engine has added a few new settings to govern the timeouts (since it was affected too by the change). There is a new whitelisting option that gives at least the power to the users to decide which pages are excluded from this behavior. I honestly do believe that these setting will be added by google sooner or later. Until then we recommend our customers to switch to edge if they are affected.

You can find it in settings\system: screenshot

Upvotes: 1

Related Questions