-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overriding Module.print
does not work with -sNODERAWFS
/ -sFORCE_FILESYSTEM
#17688
Comments
Module.print
does not always work with -sNODERAWFS
/ -sFORCE_FILESYSTEM
Module.print
does not work with -sNODERAWFS
/ -sFORCE_FILESYSTEM
Eventually, WasmFS would fix this, so please don't spend too much time on this. Though, for WasmFS it looks like a $ cat <<EOT > test.c
#include <stdio.h>
int main() {
// stderr
fprintf(stderr, "hello, world!\n");
// stdout
printf("hello, world!\n");
return 0;
}
EOT
$ cat <<EOT > main.js
const Test = require('./test.js');
async function main() {
await Test({
// In an ideal world, nothing would be printed on the terminal.
print: (text) => {},
printErr: (text) => {},
});
}
main();
EOT
# pthread usage
$ emcc -O3 test.c -o test.js -s MODULARIZE=1 -sEXPORT_NAME=Test -sWASMFS -sEXIT_RUNTIME -pthread -sPROXY_TO_PTHREAD
$ node --experimental-wasm-threads --experimental-wasm-bulk-memory main.js
hello, world!
hello, world!
# Non-pthread usage
$ emcc -O3 test.c -o test.js -s MODULARIZE=1 -sEXPORT_NAME=Test -sWASMFS -sEXIT_RUNTIME
$ node main.js Should I open a new issue for that? |
The above mentioned WasmFS issue could be resolved by doing: --- a/src/worker.js
+++ b/src/worker.js
@@ -136,6 +136,11 @@ self.onmessage = (e) => {
Module['dynamicLibraries'] = e.data.dynamicLibraries;
#endif
+ {{{ makeAsmImportsAccessInPthread('print') }}} = (text) =>
+ postMessage({cmd: 'print', text: text, threadId: Module['_pthread_self']()});
+ {{{ makeAsmImportsAccessInPthread('printErr') }}} = (text) =>
+ postMessage({cmd: 'printErr', text: text, threadId: Module['_pthread_self']()});
+
{{{ makeAsmImportsAccessInPthread('wasmMemory') }}} = e.data.wasmMemory;
#if LOAD_SOURCE_MAP To ensure that the emscripten/src/library_pthread.js Lines 274 to 277 in 85412cb
Alternatively, we could proxy the --- a/src/library.js
+++ b/src/library.js
@@ -3355,6 +3355,7 @@ mergeInto(LibraryManager.library, {
},
_emscripten_out__sig: 'vp',
+ _emscripten_out__proxy: 'sync',
_emscripten_out: function(str) {
#if ASSERTIONS
assert(typeof str == 'number');
@@ -3363,6 +3364,7 @@ mergeInto(LibraryManager.library, {
},
_emscripten_err__sig: 'vp',
+ _emscripten_err__proxy: 'sync',
_emscripten_err: function(str) {
#if ASSERTIONS
assert(typeof str == 'number'); I'm not sure what's preferred. Perhaps there's a better way to resolve this? |
To make sure I understand - is the issue that the user overrides 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 |
When running on a pthread, none of the incoming parameters on the module object are present. Proxy known handlers back to the main thread if specified. This simplifies the previous `onAbort` pthread logic for a more generic solution that also supports the `onExit`, `print` and `printErr` handlers. See: emscripten-core#17688.
Yes, exactly. I noticed that Lines 458 to 467 in fc3a217
I just opened PR #17925 for this. |
When running on a pthread, none of the incoming parameters on the module object are present. Proxy known handlers back to the main thread if specified. This simplifies the previous `onAbort` pthread logic for a more generic solution that also supports the `onExit`, `print` and `printErr` handlers that can be overridden on the incoming module. See: emscripten-core#17688.
When running on a pthread, none of the incoming parameters on the module object are present. Proxy known handlers back to the main thread if specified. This simplifies the previous `onAbort` pthread logic for a more generic solution that also supports the `onExit`, `print` and `printErr` handlers that can be overridden on the incoming module. See: emscripten-core#17688.
When running on a pthread, none of the incoming parameters on the module object are present. Proxy known handlers back to the main thread if specified. This simplifies the previous `onAbort` pthread logic for a more generic solution that also supports the `onExit`, `print` and `printErr` handlers that can be overridden on the incoming module. See: emscripten-core#17688.
When running on a pthread, none of the incoming parameters on the module object are present. Proxy known handlers back to the main thread if specified. This simplifies the previous `onAbort` pthread logic for a more generic solution that also supports the `onExit`, `print` and `printErr` handlers that can be overridden on the incoming module. See: #17688.
I believe this was fixed by #17925 ? Please reopen if not. |
Previously, when linking with -sNODERAWFS, the stdin, stdout and stderr streams were respectively attached to file descriptors 0, 1 and 2 of the running Node process. This implicitly means that overriding the print, printErr, stdin, stdout and stderr handlers on the incoming module wasn't possible. This commit ensures that stdin/stdout/stderr uses the library_tty.js implementation for its printing, which leverages the console object, whenever the above handlers weren't overriden. This matches what is done when filesystem support isn't included. Resolves: emscripten-core#17688.
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. |
Previously, when linking with -sNODERAWFS, the stdin, stdout and stderr streams were respectively attached to file descriptors 0, 1 and 2 of the running Node process. This implicitly means that overriding the print, printErr, stdin, stdout and stderr handlers on the incoming module wasn't possible. This commit ensures that stdin/stdout/stderr uses the library_tty.js implementation for its printing, which leverages the console object, whenever the above handlers weren't overriden. This matches what is done when filesystem support isn't included. Resolves: emscripten-core#17688. Resolves: emscripten-core#22264.
Previously, when linking with -sNODERAWFS, the stdin, stdout and stderr streams were respectively attached to file descriptors 0, 1 and 2 of the running Node process. This implicitly means that overriding the print, printErr, stdin, stdout and stderr handlers on the incoming module wasn't possible. This commit ensures that stdin/stdout/stderr uses the library_tty.js implementation for its printing, which leverages the console object, whenever the above handlers weren't overriden. This matches what is done when filesystem support isn't included. Resolves: emscripten-core#17688. Resolves: emscripten-core#22264.
Description:
It looks like
Module.print
does not always work with-sNODERAWFS
. A simpleprintf("hello, world!\n");
program works, but when the program actually makes use of the file system (e.g. doing afopen()
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 simpleprintf()
call.Reproduction:
See commit kleisauke@6dac36a for a test case, with that just run:
Or if you prefer a standalone example:
Expected behavior:
Overriding
Module.print
should work and nothing should print to the terminal.Actual behavior:
Version of emscripten/emsdk:
(but probably older versions also have the same bug)
Downstream issue: kleisauke/wasm-vips#23
The text was updated successfully, but these errors were encountered: