Skip to content

large timer queue after window focus blocks UI for extended time #2211

Closed

Description

@nickbeckenham

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?

For an example (using the charting library c3.js): http://jsfiddle.net/solgar/phLqwrmb/

Activity

  1. added
    bug
    on Feb 7, 2015
  2. added this to the 3.5.x milestone on Feb 7, 2015
  3. mbostock commented on Feb 7, 2015

    @mbostock
    Member

    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.)

  4. nickbeckenham commented on Mar 23, 2015

    @nickbeckenham
    Author

    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.

  5. mbostock commented on Mar 24, 2015

    @mbostock
    Member

    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.

  6. typeofweb commented on Jun 24, 2015

    @typeofweb

    Any news on this issue?

  7. modified the milestones: 3.5.7, 3.5.x on Oct 22, 2015
  8. mbostock commented on Oct 25, 2015

    @mbostock
    Member

    It’s possible that #2600 may help this issue, but I’m not sure.

  9. mbostock commented on Oct 28, 2015

    @mbostock
    Member

    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:

    1. 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.
    2. 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.
    3. 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.
    4. 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.

  10. mbostock commented on Oct 28, 2015

    @mbostock
    Member

    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…)

  11. mbostock commented on Oct 28, 2015

    @mbostock
    Member

    Also, option 4a is something we can do in a patch release, so that’s nice. 😁

  12. mbostock commented on Nov 8, 2015

    @mbostock
    Member

    Here’s a quick way to avoid this problem by not doing any work when the page isn’t visible. Replace code like this:

    setInterval(function() {
      d3.transition()…
    }, 1000);

    With this:

    setInterval(function() {
      if (document.hidden) return;
      d3.transition()…
    }, 1000);
  13. modified the milestones: 3.5.x, 3.5.7 on Nov 8, 2015
  14. added a commit that references this issue on Feb 10, 2016
  15. mbostock commented on Mar 1, 2016

    @mbostock
    Member

    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.

  16. modified the milestones: 4.0, 3.5.x on Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions