Repository navigation
Closed
Closed
Copy link
Description
Description:
It looks like Module.print does not always work with -sNODERAWFS. A simple printf("hello, world!\n"); program works, but when the program actually makes use of the file system (e.g. doing a fopen() call), Module.print is ignored and the output is directly written to the terminal. Forcing the full file system with -sFORCE_FILESYSTEM also reproduces this with a simple printf() call.
Reproduction:
See commit kleisauke@6dac36a for a test case, with that just run:
$ ./test/runner other.test_noderawfs_override_standard_streams -vOr if you prefer a standalone example:
$ cat <<EOT >> pre.js
let stdout = '';
let stderr = '';
Module['print'] = function(text) {
stdout += text;
}
Module['printErr'] = function(text) {
stderr += text;
}
Module['postRun'] = function() {
assert(stderr == '', 'stderr should be empty. \\n' +
'stderr: \\n' + stderr);
assert(stdout.startsWith('hello, world!'), 'stdout should start with the famous greeting. \\n' +
'stdout: \\n' + stdout);
}
EOT
$ emcc test/hello_world.c -o hello_world.js -sNODERAWFS -sFORCE_FILESYSTEM --pre-js pre.js
$ node hello_world.jsExpected behavior:
Overriding Module.print should work and nothing should print to the terminal.
Actual behavior:
hello, world!
/home/kleisauke/hello_world.js:162
throw ex;
^
RuntimeError: Aborted(Assertion failed: stdout should start with the famous greeting.
stdout:
)
at abort (/home/kleisauke/hello_world.js:947:11)
at assert (/home/kleisauke/hello_world.js:478:5)
at Module.postRun (/home/kleisauke/hello_world.js:35:5)
at callRuntimeCallbacks (/home/kleisauke/hello_world.js:1196:26)
at postRun (/home/kleisauke/hello_world.js:796:3)
at doRun (/home/kleisauke/hello_world.js:5324:5)
at run (/home/kleisauke/hello_world.js:5337:5)
at runCaller (/home/kleisauke/hello_world.js:5245:19)
at removeRunDependency (/home/kleisauke/hello_world.js:912:7)
at receiveInstance (/home/kleisauke/hello_world.js:1079:5)
Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.19 (a5f7b1c9a1c74a830bebedc122d204800ce8dff9)
clang version 16.0.0 (https://github.com/llvm/llvm-project 30171e76f0e5ea8037bc4d1450dd3e12af4d9938)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/kleisauke/emsdk/upstream/bin(but probably older versions also have the same bug)
Downstream issue: kleisauke/wasm-vips#23
Reacted by Anton
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Overriding `Module.print` does not always work with `-sNODERAWFS` / `-sFORCE_FILESYSTEM`[/-][+]Overriding `Module.print` does not work with `-sNODERAWFS` / `-sFORCE_FILESYSTEM`[/+]kleisauke commentedon Aug 25, 2022
Eventually, WasmFS would fix this, so please don't spend too much time on this. Though, for WasmFS it looks like a
printf()in a pthread would always be printed on the terminal, see for example:Should I open a new issue for that?
kleisauke commentedon Aug 26, 2022
The above mentioned WasmFS issue could be resolved by doing:
To ensure that the
Module['print']andModule['printErr']functions exist in a pthread andpostMessageit back to the main thread:emscripten/src/library_pthread.js
Lines 274 to 277 in 85412cb
Alternatively, we could proxy the
_emscripten_outand_emscripten_errfunctions back to the main thread by doing this:I'm not sure what's preferred. Perhaps there's a better way to resolve this?
kripken commentedon Aug 26, 2022
To make sure I understand - is the issue that the user overrides
Module.print, but that only happens on the main thread, as worker JS contexts don't run that user JS code that setsModule.print(in fact it might be connected to the HTML, which is only on the main thread). So printing on workers uses the default print path. Is that right?I think that is a real issue, if so. On the one hand, proxying is generally the right thing to do, as in the last diff. Can it be async, though? That would avoid possible deadlocks. On the other hand, 99% of the time
Module.printis not overridden, so the proxying overhead would be wasted... but trying to avoid it is tricky.Proxy known handlers back to the main thread
kleisauke commentedon Sep 25, 2022
Yes, exactly. I noticed that
onAborthad also a similar issue:emscripten/src/preamble.js
Lines 458 to 467 in fc3a217
I just opened PR #17925 for this.
Proxy known handlers back to the main thread
Proxy known handlers back to the main thread
Proxy known handlers back to the main thread
Proxy known handlers back to the main thread (#17925)
kripken commentedon Nov 4, 2022
I believe this was fixed by #17925 ? Please reopen if not.
Ensure that stdin/stdout/stderr are not mapped 1:1 under NODERAWFS
kleisauke commentedon Nov 8, 2022
Oh, sorry I re-used this issue for a similar WasmFS issue I found while debugging this.
Re-opening since the underlying issue isn't fixed, I just opened PR #18163 for this.
Ensure that stdin/stdout/stderr are not mapped 1:1 under NODERAWFS
Ensure that stdin/stdout/stderr are not mapped 1:1 under NODERAWFS
Ensure that stdin/stdout/stderr are not mapped 1:1 under NODERAWFS (e…