Repository navigation
Conversation
…ion is thrown by callbacks (see d3#2415) — but does not cover all liabilities, e.g. exceptions in event handlers
|
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. |
|
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 ;-) |
|
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. |
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.timerit 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 tod3.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:
Thoughts?