MathJax hangs with 100% CPU when resource fails to load
Created by: adamwulf
MathJax hangs with 100% CPU if one if its files fails to load. My example uses mtable.js below, but I've seen this happen with other scripts failing to load as well.
I originally found this issue on 2.7.1, and was also able to reproduce the issue 100% of the time with the current master branch (commit 2965eab5).
The test case can be found here: http://www.wikipediacorpus.com/Chapter1.html - Opening that URL will cause MathJax to hang in an infinite loop, and show the beachball cursor in Mac Safari until the browser kills the script.
The issue seems to occur when the browser fails to load a supporting resource file for some reason. In the example URL, i forced /MathJax/jax/output/HTML-CSS/autoload/mtable.js
to 404 by renaming it to mtable2.js
on the server. I'm purposefully causing the break here, but there's tons of reasons why a browser might fail to load a resource, which explains why #548 (closed) may have been so hard to reproduce.
When that file fails to load, the processOutput function circles in an infinite loop, continually trying to restart. I've added a console.log() to that method in the test URL above, which prints out the state.i value, and you can see in the JavaScript console that the value never increments above 7. The line that is throwing the restart error is line 2334:
result = MathJax.OutputJax[jax.outputJax].Process(script,state);
As a workaround, the following seems to fix the issue:
try{
result = MathJax.OutputJax[jax.outputJax].Process(script,state);
}catch(e){
if(state.i == state.lastErr){
state.i++;
console.log("state.i: " + state.i);
}else{
state.lastErr = state.i;
console.log("other: " + state.i);
}
throw e;
}
Otherwise, that line throws a restart Error, having never incremented state.i, and the whole process repeats itself.
I ran into this issue while working on MacDown, a native markdown client for Mac, which uses MathJax in its preview window (issue 807). On every edit, the preview window is reloaded, which occasionally causes scripts to fail to load, which sometimes causes this infinite recursion, hanging the app.