You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
large timer queue after window focus blocks UI for extended time #2211
I have an issue where I have ~4-5 d3.js charts updating every 1-2 seconds on my page.
When the user blurs the window (minimizes or switches to another tab), the "requestAnimationFrame" action is paused and so all transitions during this time start queueing up on the timer queue.
When the user refocuses on the window, it seems that the queue is extremely large and can block the UI anywhere from 5s all the way up to many minutes (or the tab crashes from out of memory or non responsive script).
I realise there is the selection.interrupt function which should resolve this, because really its only 1 transition per chart all queued up over and over. However should there be a more elegant way of d3.js handling this use case if there are many transitions/timers queued up/overdue when requestAnimationFrame runs? e.g. if queue is extremely long go through all scheduled transitions and remove all but the most recent for that element or perhaps it can "yield" (for want of a better word) every N transitions?
Well, all requestAnimationFrame callbacks are paused, but we do use setTimeout for longer delays, so it’s possible you’re getting some callbacks in the background. (Though, if you’re using setInterval or setTimeout yourself outside of D3, then you’d need to fix your own code first.)
Thanks, I'm not sure it is a bug in d3 per se, more something that would be nice to have for d3 to defend against bad code... (in this case an issue in c3.js which is being worked on).
But as you mentioned, you avoid requestAnimationFrame callbacks in this scenario, so it sounds like d3 is already trying to defend against it. I'm happy for this to be closed unless you think there is something you'd want to change relating to this issue.
I’d like to keep this open, but I haven’t had time to investigate this, myself. I have seen the problem occur, but I’m not sure what’s causing it or whether it’s (at least, easily) fixable.
Here’s what I think is going on, at least in Chrome:
setInterval continues to run in a background tab, causing transitions to be scheduled periodically, though perhaps at less than the requested frequency.
requestAnimationFrame does not run at all in the background, so those transitions do not start. Even zero-delay transitions wait until the first tick to initialize tweens and start, so as to give you time to configure the transition.
When the tab moves to the foreground, the timer queue is full of scheduled zero-delay transitions ready to start. Those transitions are processed in-order, causing the tweens to be initialized, but every transition except the last is immediately interrupted by the subsequent transition in the queue.
Unfortunately, calling selection.interrupt before starting the transition has no effect because those zero-delay transitions have not yet started due to being in the background; selection.interrupt only interrupts the active transition.
I believe the delay (and out-of-memory error) is primarily caused by tween initialization of the scheduled transitions that are immediately interrupted on start. Tween initialization can be expensive since it involves reading computed values from the DOM and the creation of interpolators.
Possible (non-exclusive) solutions:
Provide a way to cancel scheduled transitions synchronously. I believe the right approach is to change the behavior of selection.interrupt so that it cancels scheduled transitions as well as interrupts the active transition (if any); see interrupt should cancel all scheduled transitions, too. d3-transition#9. (An alternative would be a selection.cancel method.) This change would not be a backwards-compatible change and should wait until 4.0. Note that this requires the explicit use of selection.interrupt; if you forget to interrupt before starting a transition, you’ll still have a large timer queue. This change has the nice behavior that it also prevents the memory leak, since selection.interrupt could remove the previously-registered timers and their associated memory.
Reverse the order of timer callbacks so that they are last-in-first-out (as they were previously) rather than first-in-first-out. This way, the last transition will start first and cancel all other transitions, without giving their tweens a chance to initialize. However, I think this is a nonstarter because the order of timer callbacks should be predictable and switching to reverse order leads to confusion. Also, this approach could still run out of memory since nothing is cleaned up until the background tab returns to the foreground.
Have transitions check if they have been pre-empted by a newer transition on startup, and have them self-terminate before initializing tweens. This functions similarly to approach 2, but without changing the ordering of timer callbacks. Like approach 2, this approach could still run out of memory since nothing is cleaned up until the background tab returns to the foreground. Also, I don’t know if it’s strictly possible for a transition to check if it’s been pre-empted by another transition, since a transition delay can be changed anytime between being scheduled and the next tick. But… in practice, it’s probably safe.
Have d3.timer use setTimeout instead of requestAnimationFrame when in a background tab (perhaps using the page visibility API), so that it occasionally gets woken up in the background and can flush the queue. Equivalently, we could use a setInterval so that d3.timer gets woken up at least once per second (without needing to check page visibility, which is a relatively recent standard), rather than being paused entirely when in the background. It’s a little unfortunate to wake up when you don’t need to wake up, but the asymmetry between requestAnimationFrame and setTimeout could lead to a memory leak, so this is probably a good idea.
I think approaches 1 and 4 are probably worth doing.
Also, option 4a, a less invasive change to detect the asymmetry: when a new timer is registered, check the last time you requested a tick; if that time is more than (say) 1s ago, use setTimeout rather than requestAnimationFrame to request the next tick. This way, only the first tick of a timer is treated specially, and subsequent ticks are still entirely paused in the background.
(Well, maybe this wouldn’t work if you just call d3.timer once from the background, though…)
As for d3-timer 0.3.2, timer is frozen in the background, such that when the page returns to the foreground, it’s like nothing happened. In addition, there are new d3.timeout and d3.interval methods which are replacements for setTimeout and setInterval that are likewise paused in the background.
Activity
mbostock commentedon Feb 7, 2015
Well, all requestAnimationFrame callbacks are paused, but we do use setTimeout for longer delays, so it’s possible you’re getting some callbacks in the background. (Though, if you’re using setInterval or setTimeout yourself outside of D3, then you’d need to fix your own code first.)
nickbeckenham commentedon Mar 23, 2015
Thanks, I'm not sure it is a bug in d3 per se, more something that would be nice to have for d3 to defend against bad code... (in this case an issue in c3.js which is being worked on).
But as you mentioned, you avoid requestAnimationFrame callbacks in this scenario, so it sounds like d3 is already trying to defend against it. I'm happy for this to be closed unless you think there is something you'd want to change relating to this issue.
mbostock commentedon Mar 24, 2015
I’d like to keep this open, but I haven’t had time to investigate this, myself. I have seen the problem occur, but I’m not sure what’s causing it or whether it’s (at least, easily) fixable.
typeofweb commentedon Jun 24, 2015
Any news on this issue?
mbostock commentedon Oct 25, 2015
It’s possible that #2600 may help this issue, but I’m not sure.
mbostock commentedon Oct 28, 2015
Here’s what I think is going on, at least in Chrome:
Unfortunately, calling selection.interrupt before starting the transition has no effect because those zero-delay transitions have not yet started due to being in the background; selection.interrupt only interrupts the active transition.
I believe the delay (and out-of-memory error) is primarily caused by tween initialization of the scheduled transitions that are immediately interrupted on start. Tween initialization can be expensive since it involves reading computed values from the DOM and the creation of interpolators.
Possible (non-exclusive) solutions:
I think approaches 1 and 4 are probably worth doing.
mbostock commentedon Oct 28, 2015
Also, option 4a, a less invasive change to detect the asymmetry: when a new timer is registered, check the last time you requested a tick; if that time is more than (say) 1s ago, use setTimeout rather than requestAnimationFrame to request the next tick. This way, only the first tick of a timer is treated specially, and subsequent ticks are still entirely paused in the background.
(Well, maybe this wouldn’t work if you just call d3.timer once from the background, though…)
mbostock commentedon Oct 28, 2015
Also, option 4a is something we can do in a patch release, so that’s nice. 😁
mbostock commentedon Nov 8, 2015
Here’s a quick way to avoid this problem by not doing any work when the page isn’t visible. Replace code like this:
With this:
Fix #6 - a slow poke for paused animations.
mbostock commentedon Mar 1, 2016
This has been fixed in d3-timer for D3 4.0.
As for d3-timer 0.3.2, timer is frozen in the background, such that when the page returns to the foreground, it’s like nothing happened. In addition, there are new d3.timeout and d3.interval methods which are replacements for setTimeout and setInterval that are likewise paused in the background.