Skip to content

Make transitions and d3.timer more robust - #2416

Closed
natevw wants to merge 2 commits into
d3:masterfrom
natevw:nvw-transition-robustness
Closed

natevw wants to merge 2 commits into
d3:masterfrom
natevw:nvw-transition-robustness

Conversation

@natevw

@natevw natevw commented May 4, 2015

Copy link
Copy Markdown
Contributor

This addresses some of the places where an exception from a callback can corrupt the internal state of d3.timer and/or d3_transitionNode, resulting in the issue at #2415.

I'm submitting this patch as it does improve the situation, but to be honest it's not a full fix, nor a particularly elegant one. Most of D3 can get away with leaving exceptions to blast their way through, and I think that's a good design. Here, however, seems to be a slippery slope of needing to catch and delayed-throw any possible error to keep the mechanism on track.

The timer/transition logic can get in a really bad state depending on where the exception is thrown. If an exception hits d3.timer it simply breaks permanently, no more timers fire whether existing or registered later. Fixing that reveals a worse situation though: the transition logic monkey patches its own (internal to d3.timer!) callback, and usually only after it has called its own callbacks [tweens, factories, event handlers…] — so the internal transition logic stops advancing properly if it doesn't handle exceptions, but it keeps getting triggered triggered triggered triggered forever anyway now that d3.timer does.

Part of me still wants to fix this (thus this start), but I'd like some feedback first on what the best direction is:

  • give up and say timer/tween callbacks must not ever throw. drawback: if any one caller breaks that rule, all others suffer.
  • have d3.timer catch but also automatically cancel any of its callbacks that throw. drawback: means transition does not complete, probably leaving its selection/elements corrupted still.
  • wrap every single call of d3_transitionNode to any external tween/factory/event(/etc.?) handler with its own try/catch wart
  • overhaul d3_transitionNode to be more naturally robust, somehow.

Thoughts?

natevw added 2 commits May 4, 2015 13:43
…ion is thrown by callbacks (see d3#2415) — but does not cover all liabilities, e.g. exceptions in event handlers
@mbostock

mbostock commented May 5, 2015

Copy link
Copy Markdown
Member

One reason I didn’t do this is fear of performance degradation. I believe try-catch can disable certain optimizations by popular JavaScript engines. And this is the most performance-sensitive part of D3.

Now, I do use try-finally inside event dispatching (see src/event/event.js and src/selection/on.js), so I’m not sure if those optimizations are already precluded, or if it’s better to put the try-catch higher up the stack to allow the inner loop to be optimized. This is all kind of black magic because most of what matters is touching the DOM, unless you have lots of timers.

I haven’t had time to think this through but I also wonder whether it would be possible for the exception to be thrown “naturally”, which could prevent some timers from being called back during that iteration, and to change the code so that any exception would not affect subsequent timer callbacks—those would resume normally on the next animation frame. That way, an exception could cause localized damage, but not permanently break d3.timer or selection.transition.

@natevw

natevw commented May 5, 2015

Copy link
Copy Markdown
Contributor Author

Ah, good point with the exception handling (at least in V8, "functions containing try/catch are not optimized").

I think what you're saying about letting exceptions be thrown naturally, is the same as I meant by "overhaul to be more robust". That does seem the way to go, then, though I can't really volunteer myself — after diving in this far, I decided the right fix for my client was a trivial change to stop a particular bit of app code from throwing in the first place ;-)

@mbostock mbostock added the bug Something isn’t working label Oct 21, 2015
@mbostock mbostock added this to the 3.5.x milestone Oct 21, 2015
@mbostock

Copy link
Copy Markdown
Member

I filed d3/d3-timer#4 to track this for 4.0, but I’m guessing we probably won’t make this proposed change to 3.x. I’ll leave open #2415 for now.

@mbostock mbostock closed this Oct 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn’t working

Development

Successfully merging this pull request may close these issues.

2 participants