Skip to content

Allow late print calls from terminating pthreads#27012

Open
brendandahl wants to merge 1 commit into
emscripten-core:mainfrom
brendandahl:flaky-test_pthread_print_override_modularize
Open

Allow late print calls from terminating pthreads#27012
brendandahl wants to merge 1 commit into
emscripten-core:mainfrom
brendandahl:flaky-test_pthread_print_override_modularize

Conversation

@brendandahl
Copy link
Copy Markdown
Collaborator

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 late-arriving print and printErr commands through the terminated-worker message stub. Fixes flaky test test_pthread_print_override_modularize.

Fixes #19683

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 late-arriving print and printErr commands
through the terminated-worker message stub. Fixes flaky test
test_pthread_print_override_modularize.

Fixes emscripten-core#19683
@brendandahl brendandahl requested review from juj and sbc100 May 26, 2026 22:16
Comment thread src/lib/libpthread.js
// rather than discarding them, which ensures standard output is not dropped
// on exit and avoids unexpected "received command from terminated worker"
// assertion failures.
if (cmd === {{{ CMD_CALL_HANDLER }}} && (d.handler === 'print' || d.handler === 'printErr')) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe allow this for all CMD_CALL_HANDLER?

Comment thread src/lib/libpthread.js
}
#if ASSERTIONS
var cmd = e['data'].cmd;
err(`received "${cmd}" command from terminated worker: ${worker.workerID}`);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should replace received with ignoring here? And also maybe replace err with dbg?

Comment thread src/lib/libpthread.js
@@ -596,8 +595,18 @@ var LibraryPThread = {
// 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) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should consider just removing this stubbing of the onmessage handler?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, I didn't look much at the history, but the comment made it seem important.

Comment thread src/lib/libpthread.js
// flight in the message queue of the main thread. We should execute them
// rather than discarding them, which ensures standard output is not dropped
// on exit and avoids unexpected "received command from terminated worker"
// assertion failures.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertion failures -> error logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_pthread_print_override_modularize is failing on macOS after node upgrade to v16

2 participants