Skip to content

Commit 858d13b

Browse files
authored
Make sure postRun is called after main() if main suspends (#24089)
await callMain(), which is an async function if JSPI is enabled. Fixes #12402.
1 parent 6ffe193 commit 858d13b

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/postamble.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function run() {
169169
return;
170170
}
171171

172-
function doRun() {
172+
{{{ asyncIf(ASYNCIFY == 2) }}}function doRun() {
173173
// run may have just been called through dependencies being fulfilled just in this very frame,
174174
// or while the async setStatus time below was happening
175175
#if ASSERTIONS
@@ -199,9 +199,9 @@ function run() {
199199
#if HAS_MAIN
200200
var noInitialRun = {{{ makeModuleReceiveExpr('noInitialRun', !INVOKE_RUN) }}};
201201
#if MAIN_READS_PARAMS
202-
if (!noInitialRun) callMain(args);
202+
if (!noInitialRun) {{{ awaitIf(ASYNCIFY == 2) }}}callMain(args);
203203
#else
204-
if (!noInitialRun) callMain();
204+
if (!noInitialRun) {{{ awaitIf(ASYNCIFY == 2) }}}callMain();
205205
#endif
206206
#else
207207
#if ASSERTIONS

test/other/hello_world_suspend.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2025 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <stdio.h>
9+
#include <emscripten.h>
10+
11+
int main() {
12+
emscripten_sleep(1);
13+
printf("hello, world!\n");
14+
return 0;
15+
}

test/test_other.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,6 +2865,13 @@ def test_prepost2(self):
28652865
self.do_runf('hello_world.c', 'pre-run\nhello, world!\npost-run\n',
28662866
emcc_args=['--pre-js', 'pre.js', '--pre-js', 'pre2.js'])
28672867

2868+
@requires_jspi
2869+
def test_prepost_jspi(self):
2870+
create_file('pre.js', 'Module.preRun = () => out("pre-run");')
2871+
create_file('pre2.js', 'Module.postRun = () => out("post-run");')
2872+
self.do_runf('other/hello_world_suspend.c', 'pre-run\nhello, world!\npost-run\n',
2873+
emcc_args=['--pre-js', 'pre.js', '--pre-js', 'pre2.js', '-sJSPI'])
2874+
28682875
def test_prepre(self):
28692876
create_file('pre.js', '''
28702877
Module.preRun = [() => out('pre-run-0'), () => out('pre-run-1')];

0 commit comments

Comments
 (0)