From 49dc0c75f697ee820178fc9bf9b5a03b9c3cb075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Wed, 25 Nov 2026 21:28:07 +0200 Subject: [PATCH] [emrun] Allow --emrun together with -sEXIT_RUNTIME=0 Linking with --emrun has been an error together with an explicit -sEXIT_RUNTIME=0 since #23096. Before that, --emrun silently enabled EXIT_RUNTIME even when it was explicitly disabled. Make EXIT_RUNTIME=1 only the default of --emrun, and honor an explicit -sEXIT_RUNTIME=0. emrun_postjs.js needs the runtime to exit only for reporting the exit code of the program to emrun. The capture of stdout and stderr does not depend on it, and emrun also quits when the browser is closed. So a page that keeps running after main() has returned, e.g. a test that finishes in an asynchronous callback, can be run under emrun and end the run by closing its window. With -sEXIT_RUNTIME=0 the callback that emrun_postjs.js registers with addOnExit() is never called. Co-Authored-By: Claude Fable 5.2 --- ChangeLog.md | 5 +++++ docs/emcc.txt | 7 +++++-- site/source/docs/tools_reference/emcc.rst | 2 +- test/test_other.py | 12 ++++++++++++ tools/link.py | 7 +++---- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index be896eeec663f..7b16fcb210938 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -35,6 +35,11 @@ See docs/process.md for more on how version tagging works. are now moved correctly into `-sLEGACY_GL_EMULATION` mode. In GLES and WebGL, use the correct API functions `glClearDepthf()` and `glDepthRangef()` instead. (#28379) +- The `--emrun` linker flag can again be used together with `-sEXIT_RUNTIME=0`, + which was made an error in 3.1.74. `--emrun` still enables `EXIT_RUNTIME` by + default, but if it is explicitly disabled, the runtime is now kept alive + after `main()` returns. `emrun` then captures `stdout` and `stderr` as + before, but the page does not report an exit code to it. (#28407) 7.0.1 - 11/21/26 ---------------- diff --git a/docs/emcc.txt b/docs/emcc.txt index 97e3900648682..1ca553ec5bf0e 100644 --- a/docs/emcc.txt +++ b/docs/emcc.txt @@ -525,8 +525,11 @@ Options that are modified or new in *emcc* are listed below: [link] Enables the generated output to be aware of the emrun command line tool. This allows "stdout", "stderr" and "exit(returncode)" capture when running the generated application - through *emrun*. (This enables *EXIT_RUNTIME=1*, allowing normal - runtime exiting with return code passing.) + through *emrun*. (This enables *EXIT_RUNTIME=1* by default, + allowing normal runtime exiting with return code passing. If + "-sEXIT_RUNTIME=0" is passed explicitly, "stdout" and "stderr" are + still captured, but the application does not report an exit code to + *emrun*.) "--cpuprofiler" [link] Embeds a simple CPU profiler onto the generated page. Use diff --git a/site/source/docs/tools_reference/emcc.rst b/site/source/docs/tools_reference/emcc.rst index 77430b77124dd..1240b98986fb8 100644 --- a/site/source/docs/tools_reference/emcc.rst +++ b/site/source/docs/tools_reference/emcc.rst @@ -525,7 +525,7 @@ Options that are modified or new in *emcc* are listed below: ``--emrun`` [link] - Enables the generated output to be aware of the :ref:`emrun ` command line tool. This allows ``stdout``, ``stderr`` and ``exit(returncode)`` capture when running the generated application through *emrun*. (This enables `EXIT_RUNTIME=1`, allowing normal runtime exiting with return code passing.) + Enables the generated output to be aware of the :ref:`emrun ` command line tool. This allows ``stdout``, ``stderr`` and ``exit(returncode)`` capture when running the generated application through *emrun*. (This enables `EXIT_RUNTIME=1` by default, allowing normal runtime exiting with return code passing. If ``-sEXIT_RUNTIME=0`` is passed explicitly, ``stdout`` and ``stderr`` are still captured, but the application does not report an exit code to *emrun*.) ``--cpuprofiler`` [link] diff --git a/test/test_other.py b/test/test_other.py index e85668716a871..e802b80755aed 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -5042,6 +5042,18 @@ def test(cxx, no_exit, assertions, flush=0, keepalive=0, filesystem=1): def test_no_exit_runtime_strict(self): self.do_other_test('test_no_exit_runtime_strict.c', cflags=['-sSTRICT']) + @parameterized({ + '': ([], True), + 'exit_runtime': (['-sEXIT_RUNTIME'], True), + 'no_exit_runtime': (['-sEXIT_RUNTIME=0'], False), + }) + def test_emrun_exit_runtime(self, args, expect_exit): + # --emrun enables EXIT_RUNTIME by default, so that the page can report its + # exit code to emrun, but EXIT_RUNTIME can still be explicitly disabled. + self.run_process([EMCC, test_file('core/test_atexit.c'), '--emrun'] + args) + output = self.run_js('a.out.js') + self.assertContainedIf('B\nA\n', output, expect_exit) + def test_extra_opt_levels(self): # Opt levels that we don't tend to test elsewhere for opt in ('-Og', '-Ofast'): diff --git a/tools/link.py b/tools/link.py index 747e6c4e221fa..960090b7a3df5 100644 --- a/tools/link.py +++ b/tools/link.py @@ -873,10 +873,9 @@ def phase_linker_setup(linker_args): # ruff: ignore[complex-structure, too-many options.post_js.append(utils.path_from_root('src/emrun_postjs.js')) if settings.MINIMAL_RUNTIME: exit_with_error('--emrun is not compatible with MINIMAL_RUNTIME') - # emrun mode waits on program exit - if user_settings.get('EXIT_RUNTIME') == '0': - exit_with_error('--emrun is not compatible with EXIT_RUNTIME=0') - settings.EXIT_RUNTIME = 1 + # emrun mode waits on program exit, unless the user explicitly disabled + # EXIT_RUNTIME. In that case the page does not report an exit code to emrun. + default_setting('EXIT_RUNTIME', 1) # emrun_postjs.js needs this library function. settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$addOnExit']