Skip to content

Commit e2e5787

Browse files
committed
Call __stdio_exit rather than fflush
See musl commit a71e0af25544fd2486e57ea51c6d05abdbf44c3e.
1 parent bf592e8 commit e2e5787

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

emcc.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,11 +1899,17 @@ def default_setting(name, new_default):
18991899
]
19001900

19011901
if settings.FILESYSTEM and not settings.STANDALONE_WASM:
1902-
# to flush streams on FS exit, we need to be able to call fflush
1903-
# we only include it if the runtime is exitable, or when ASSERTIONS
1902+
# To flush streams on FS exit, we need to be able to call __stdio_exit,
1903+
# this is a streamlined fflush variant that avoids performing any unnecessary
1904+
# operations and which never unlocks the files or open file list, so we can
1905+
# be sure no other threads write new data to a stream's buffer after it's
1906+
# already flushed. This function is only included if the runtime is exitable.
1907+
if settings.EXIT_RUNTIME:
1908+
settings.EXPORTED_FUNCTIONS += ['___stdio_exit']
1909+
# Include the fflush function when ASSERTIONS is enabled.
19041910
# (ASSERTIONS will check that streams do not need to be flushed,
19051911
# helping people see when they should have enabled EXIT_RUNTIME)
1906-
if settings.EXIT_RUNTIME or settings.ASSERTIONS:
1912+
if settings.ASSERTIONS:
19071913
settings.EXPORTED_FUNCTIONS += ['_fflush']
19081914

19091915
if settings.SUPPORT_ERRNO:

src/library_fs.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,9 +1514,10 @@ FS.staticInit();` +
15141514
},
15151515
quit: function() {
15161516
FS.init.initialized = false;
1517-
// force-flush all streams, so we get musl std streams printed out
1518-
var fflush = Module['_fflush'];
1519-
if (fflush) fflush(0);
1517+
// ensure the musl std streams are printed out by
1518+
// calling the streamlined fflush variant
1519+
var stdio_exit = Module['___stdio_exit'];
1520+
if (stdio_exit) stdio_exit();
15201521
// close all of our streams
15211522
for (var i = 0; i < FS.streams.length; i++) {
15221523
var stream = FS.streams[i];

src/library_wasi.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,14 @@ var WasiLibrary = {
182182

183183
#if SYSCALLS_REQUIRE_FILESYSTEM == 0 && (!MINIMAL_RUNTIME || EXIT_RUNTIME)
184184
$flush_NO_FILESYSTEM: function() {
185-
// flush anything remaining in the buffers during shutdown
185+
// flush anything remaining in the buffers during shutdown.
186+
// Only call the streamlined fflush variant when ASSERTIONS are disabled
187+
// to ensure that the warning for unflushed content is still displayed.
188+
#if ASSERTIONS
186189
if (typeof _fflush !== 'undefined') _fflush({{{ pointerT(0) }}});
190+
#else
191+
if (typeof ___stdio_exit !== 'undefined') ___stdio_exit();
192+
#endif
187193
var buffers = SYSCALLS.buffers;
188194
if (buffers[1].length) SYSCALLS.printChar(1, {{{ charCode("\n") }}});
189195
if (buffers[2].length) SYSCALLS.printChar(2, {{{ charCode("\n") }}});

0 commit comments

Comments
 (0)