-
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
fprintf is non-thread-safe #13194
Comments
BTW, it's not just a "cosmetic" corruption of strings, since in case the printed messages are UTF-8, this issue causes them to sometimes become incorrect UTF strings, resulting in making the messages undecipherable and hitting some internal error logs and assertions in Emscripten code. |
Additionally, after running this program several hundreds of times it failed with the following memory corruption error:
(I don't know if it's related to |
Hello, is there any update on this issue? It sounds unfortunate that the print debugging, even recommended as the first debugging means by the official doc (https://emscripten.org/docs/porting/Debugging.html#manual-print-debugging) is actually causing additional issues by itself. BTW, I ran the repro snippet quoted above under 2.0.13, and after a few runs I got an error about invalid UTF, despite that all strings printed by the program are ASCII:
So the memory corruption errors quoted above might be true, as this would at least explain how printed ASCII strings could turn into invalid UTF-8. |
That's great news; thanks for looking into this! |
Hello, is there any timeline for when those fixes (PR #13007 or it follow-ups) can be landed? For the context, we're looking into rolling out a Wasm version of an existing Chrome extension that previously used multi-threaded NaCl modules. This issue is one of blockers, since it implies that normal debug logs to stdout/stderr can cause random crashes in the field (see the memory corruption log quoted above). |
Sorry for the delay. I'm a bit out of bandwidth lately, it would be great if one of the Emscripten maintainers could land that separately, otherwise I'll take a look at it this week. As a possible workaround, you could use |
I had some time today and opened a PR for your fixes for this issue @kleisauke , #13837 . Verified as working perfectly, thanks! |
Hello, can the same issue happen to other string streams (like std::ostringstream)? |
This is a slightly modified version of d5d5f69 The change to __lockfile.c means we always compile in the locking code but atomics will get lowered away in single threaded builds and the wait/wake functions are no-ops in this mode too, so it should not effect code size in release builds. Because __lockfile.c is not compiled into a multi-thread-aware library (its not part of an MTLibrary) it was never actually being built with `__EMSCRIPTEN_PTHREADS__`. Fixes: #13194
This is second attempt at landing a version of d5d5f69 The first time we tried it #13837 we ran into issues with test_pthread_exit_process deadlocking which I tracked down to an issue with `system/lib/libc/musl/src/thread/__wait.c` where it was blocking the main thread forever rather then looping and calling `emscripten_main_thread_process_queued_calls`. Fixes: #13194
This is second attempt at landing a version of d5d5f69 The first time we tried it #13837 we ran into issues with test_pthread_exit_process deadlocking which I tracked down to an issue with `system/lib/libc/musl/src/thread/__wait.c` where it was blocking the main thread forever rather then looping and calling `emscripten_main_thread_process_queued_calls`. Includes a version of #14481 so that should land before this does. Fixes: #13194
This is second attempt at landing a version of d5d5f69 The first time we tried it #13837 we ran into issues with test_pthread_exit_process deadlocking which I tracked down to an issue with `system/lib/libc/musl/src/thread/__wait.c` where it was blocking the main thread forever rather then looping and calling `emscripten_main_thread_process_queued_calls`. Includes a version of #14481 so that should land before this does. Fixes: #13194
This is second attempt at landing a version of d5d5f69 The first time we tried it #13837 we ran into issues with test_pthread_exit_process deadlocking which I tracked down to an issue with `system/lib/libc/musl/src/thread/__wait.c` where it was blocking the main thread forever rather then looping and calling `emscripten_main_thread_process_queued_calls`. Includes a version of #14481 so that should land before this does. Fixes: #13194
This is second attempt at landing a version of d5d5f69 The first time we tried it #13837 we ran into issues with test_pthread_exit_process deadlocking which I tracked down to an issue with `system/lib/libc/musl/src/thread/__wait.c` where it was blocking the main thread forever rather then looping and calling `emscripten_main_thread_process_queued_calls`. Includes a version of #14481 so that should land before this does. Fixes: #13194
Also, fix Node.js compatibility while we are here. See: emscripten-core#18171 (comment)
Emscripten's implementation of
fprintf
isn't thread-safe in the sense that lines printed from different threads may interleave with each other, producing merged/corrupted messages in the result.For example, when running the following program built under Emscripten:
with piping its result through
sort -u
, it can be seen that besides the expected 100-character lines there are often 200-/300-character ones - obviously, the lines printed from different threads got collapsed and merged together.This behavior doesn't seem to conform to the POSIX specification:
The same program compiled using the normal
g++
with the standard libc always produces only the expected 100-line characters on my Linux machine.The text was updated successfully, but these errors were encountered: