aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/postamble.js7
-rw-r--r--tests/test_other.py32
2 files changed, 35 insertions, 4 deletions
diff --git a/src/postamble.js b/src/postamble.js
index 63495914..d6c059b8 100644
--- a/src/postamble.js
+++ b/src/postamble.js
@@ -117,16 +117,15 @@ function run(args) {
preRun();
- if (runDependencies > 0) {
- // a preRun added a dependency, run will be called later
- return;
- }
+ if (runDependencies > 0) return; // a preRun added a dependency, run will be called later
+ if (Module['calledRun']) return; // run may have just been called through dependencies being fulfilled just in this very frame
function doRun() {
ensureInitRuntime();
preMain();
+ assert(!Module['calledRun']);
Module['calledRun'] = true;
if (Module['_main'] && shouldRunNow) {
Module['callMain'](args);
diff --git a/tests/test_other.py b/tests/test_other.py
index c5f0c583..f91b4683 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -2219,3 +2219,35 @@ mergeInto(LibraryManager.library, {
process.communicate()
assert(os.path.isfile(outdir + 'hello_world.obj'))
+
+ def test_doublestart_bug(self):
+ open('code.cpp', 'w').write(r'''
+#include <stdio.h>
+#include <emscripten.h>
+
+void main_loop(void) {
+ static int cnt = 0;
+ if (++cnt >= 10) emscripten_cancel_main_loop();
+}
+
+int main(void) {
+ printf("This should only appear once.\n");
+ emscripten_set_main_loop(main_loop, 10, 0);
+ return 0;
+}
+''')
+
+ open('pre.js', 'w').write(r'''
+if (typeof Module === 'undefined') Module = eval('(function() { try { return Module || {} } catch(e) { return {} } })()');
+if (!Module['preRun']) Module['preRun'] = [];
+Module["preRun"].push(function () {
+ Module['addRunDependency']('test_run_dependency');
+ Module['removeRunDependency']('test_run_dependency');
+});
+''')
+
+ Popen([PYTHON, EMCC, 'code.cpp', '--pre-js', 'pre.js']).communicate()
+ output = run_js(os.path.join(self.get_dir(), 'a.out.js'), engine=NODE_JS)
+
+ assert output.count('This should only appear once.') == 1, '\n'+output
+