Skip to content

Commit 3b43fe8

Browse files
authored
Don't call checkUnflushedContent if runtime is being kept alive (#16140)
1 parent fde3048 commit 3b43fe8

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/postamble.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,13 @@ function checkUnflushedContent() {
397397
function exit(status, implicit) {
398398
EXITSTATUS = status;
399399

400-
#if ASSERTIONS
401-
#if EXIT_RUNTIME == 0
402-
checkUnflushedContent();
403-
#endif // EXIT_RUNTIME
404-
#endif // ASSERTIONS
400+
#if ASSERTIONS && !EXIT_RUNTIME
401+
// Skip this check if the runtime is being kept alive deliberately.
402+
// For example if `exit_with_live_runtime` is called.
403+
if (!runtimeKeepaliveCounter) {
404+
checkUnflushedContent();
405+
}
406+
#endif // ASSERTIONS && !EXIT_RUNTIME
405407

406408
#if USE_PTHREADS
407409
if (!implicit) {

tests/test_other.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3578,40 +3578,63 @@ def test_no_exit_runtime_warnings_flush(self):
35783578
# check we warn if there is unflushed info
35793579
create_file('code.c', r'''
35803580
#include <stdio.h>
3581+
#include <emscripten/emscripten.h>
35813582
int main(int argc, char **argv) {
35823583
printf("hello\n");
35833584
printf("world"); // no newline, not flushed
35843585
#if FLUSH
35853586
printf("\n");
35863587
#endif
3588+
#if KEEPALIVE
3589+
emscripten_exit_with_live_runtime();
3590+
#endif
35873591
}
35883592
''')
35893593
create_file('code.cpp', r'''
35903594
#include <iostream>
3595+
#include <emscripten/emscripten.h>
35913596
int main() {
35923597
using namespace std;
35933598
cout << "hello" << std::endl;
35943599
cout << "world"; // no newline, not flushed
35953600
#if FLUSH
35963601
std::cout << std::endl;
35973602
#endif
3603+
#if KEEPALIVE
3604+
emscripten_exit_with_live_runtime();
3605+
#endif
35983606
}
35993607
''')
3600-
for compiler, src in [(EMCC, 'code.c'), (EMXX, 'code.cpp')]:
3608+
warning = 'stdio streams had content in them that was not flushed. you should set EXIT_RUNTIME to 1'
3609+
3610+
def test(cxx, no_exit, assertions, flush, keepalive):
3611+
if cxx:
3612+
cmd = [EMXX, 'code.cpp']
3613+
else:
3614+
cmd = [EMCC, 'code.c']
3615+
# TODO: also check FILESYSTEM=0 here. it never worked though, buffered output was not emitted at shutdown
3616+
print('%s: no_exit=%d assertions=%d flush=%d keepalive=%d' % (cmd[1], no_exit, assertions, flush, keepalive))
3617+
cmd += ['-sEXIT_RUNTIME=%d' % (1 - no_exit), '-sASSERTIONS=%d' % assertions]
3618+
if flush:
3619+
cmd += ['-DFLUSH']
3620+
if keepalive:
3621+
cmd += ['-DKEEPALIVE']
3622+
self.run_process(cmd)
3623+
output = self.run_js('a.out.js')
3624+
exit = 1 - no_exit
3625+
self.assertContained('hello', output)
3626+
self.assertContainedIf('world', output, exit or flush)
3627+
self.assertContainedIf(warning, output, no_exit and assertions and not flush and not keepalive)
3628+
3629+
# Run just one test with KEEPALIVE set. In this case we don't expect to see any kind
3630+
# of warning becasue we are explictly requesting the runtime stay alive for later use.
3631+
test(cxx=0, no_exit=1, assertions=1, flush=0, keepalive=1)
3632+
3633+
for cxx in [0, 1]:
36013634
for no_exit in [0, 1]:
36023635
for assertions in [0, 1]:
36033636
for flush in [0, 1]:
3604-
# TODO: also check FILESYSTEM=0 here. it never worked though, buffered output was not emitted at shutdown
3605-
print(src, no_exit, assertions, flush)
3606-
cmd = [compiler, src, '-sEXIT_RUNTIME=%d' % (1 - no_exit), '-sASSERTIONS=%d' % assertions]
3607-
if flush:
3608-
cmd += ['-DFLUSH']
3609-
self.run_process(cmd)
3610-
output = self.run_js('a.out.js')
3611-
exit = 1 - no_exit
3612-
self.assertContained('hello', output)
3613-
assert ('world' in output) == (exit or flush), 'unflushed content is shown only when exiting the runtime'
3614-
assert (no_exit and assertions and not flush) == ('stdio streams had content in them that was not flushed. you should set EXIT_RUNTIME to 1' in output), 'warning should be shown'
3637+
test(cxx, no_exit, assertions, flush, 0)
36153638

36163639
def test_fs_after_main(self):
36173640
for args in [[], ['-O1']]:

0 commit comments

Comments
 (0)