From f14aa9bd91e0d6ca583697ba60fd84161575ea55 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 26 May 2026 21:26:06 +0000 Subject: [PATCH] Allow late print calls from terminating pthreads When a pthreaded application exits under PROXY_TO_PTHREAD and EXIT_RUNTIME, the worker thread signals the main thread to exit using the system queue. However, there can also be messages from postMessage in-flight from the worker. This results in a race condition where the main thread processes the exit and terminates the worker (synchronously stubbing out its onmessage handler to discard further messages) before processing print messages already posted by the worker before exiting. As a result, valid printed output is lost, and the test fails due to unexpected assertion warnings. Resolve this by allowing messages after termination. Fixes flaky test test_pthread_print_override_modularize. Fixes #19683 --- src/lib/libpthread.js | 11 ----------- test/test_other.py | 4 ---- 2 files changed, 15 deletions(-) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index ac112b6a6dc80..1a25733df5c69 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -591,17 +591,6 @@ var LibraryPThread = { dbg(`terminateWorker: ${worker.workerID}`); #endif worker.terminate(); - // terminate() can be asynchronous, so in theory the worker can continue - // to run for some amount of time after termination. However from our POV - // the worker is now dead and we don't want to hear from it again, so we stub - // out its message handler here. This avoids having to check in each of - // the onmessage handlers if the message was coming from a valid worker. - worker.onmessage = (e) => { -#if ASSERTIONS - var cmd = e.data.cmd; - err(`received "${cmd}" command from terminated worker: ${worker.workerID}`); -#endif - }; }, _emscripten_thread_cleanup: (thread) => { diff --git a/test/test_other.py b/test/test_other.py index 0d924f90e18a6..5758d6fbb13e1 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -6166,10 +6166,6 @@ def test_modularize_instantiation_error(self): @crossplatform @requires_pthreads - @flaky('https://github.com/emscripten-core/emscripten/issues/19683') - # The flakiness of this test is very high on macOS so just disable it - # completely. - @no_mac('https://github.com/emscripten-core/emscripten/issues/19683') def test_pthread_print_override_modularize(self): self.set_setting('EXPORT_NAME', 'Test') self.set_setting('PROXY_TO_PTHREAD')