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 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.
+1
+1
You are right, failure to load a resource can cause problems like this. Unfortunately, your solution is not the right one, as MathJax uses special "restart" errors to manage loading its external resources, and restarts can occur multiple times within an expression without it representing an error (e.g., if an expression requires loading more than one file, say for an autoloaded component like the table support, and an autoloaded font file to handle unusual characters). Your technique would cause such an expression to be skipped even if the files are being loaded properly. (Also, the expression should not just be skipped, but a Math Processing Error inserted in its place.)
There are several possible ways to approach this. I suspect the best one is to modify the autoload calls in the various output jax to remove themselves before restarting so that if the file doesn't load, the file won't be requested again.
Here's a patch that does that for the CommonHTML output jax:
<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("CommonHTML Jax Ready",function () {
var CHTML = MathJax.OutputJax.CommonHTML,
MML = MathJax.ElementJax.mml,
MBASE = MML.mbase;
var AUTOLOAD = MBASE.CHTMLautoload,
AUTOLOADFILE = MBASE.CHTMLautoloadFile;
MBASE.Augment({},{
CHTMLautoload: function () {
this.toCommonHTML = MBASE.CHTMLautoloadFail;
AUTOLOAD.call(this);
},
CHTMLautoloadFail: function () {
throw Error("Can't autoload '"+this.type+"'");
},
CHTMLautoloadList: {},
CHTMLautoloadFile: function (name) {
if (MBASE.CHTMLautoloadList[name]) {
throw Error("Can't autoload file '"+name+"'");
}
MBASE.CHTMLautoloadList[name] = true;
AUTOLOADFILE.call(this,name);
}
});
});
</script>
Of course, if you are using a different output jax, you would need to modify this to refer to the proper jax.
When is this bug planned to be fixed? I think a lot of people, including myself, are waiting for it.
When is this bug planned to be fixed?
I expect it will be included in the next release. But we just released 2.7.3, so I don't expect another release until the end of the summer.
==> Merged.
== In testsuite
MathMLToDisplay/issue1936-1.html
MathMLToDisplay/issue1936-2.html
Most helpful comment
==> Merged.