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

@brendandahl brendandahl requested review from juj and sbc100 May 26, 2026 22:16
Comment thread src/lib/libpthread.js Outdated
Comment thread src/lib/libpthread.js Outdated
Comment thread src/lib/libpthread.js Outdated
@@ -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.

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.

I tried removing the stub and it seems to pass tests locally. This was added back in db35f20 and that was added as a speculative fix, so it seems like we could get rid of it.

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.

Its certainly seems possible that bad/strange things could happen if we process messages from workers that have been terminated... but I'm not sure how to balance that potential risk with the risk of not processing the messages :)

Comment thread src/lib/libpthread.js Outdated
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 emscripten-core#19683
@brendandahl brendandahl force-pushed the flaky-test_pthread_print_override_modularize branch from 8db019b to f14aa9b Compare May 29, 2026 16:30
@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented May 29, 2026

Can you update the PR description?

Can you reference the PR that this is effectively reverting?

@brendandahl
Copy link
Copy Markdown
Collaborator Author

Interestingly test_pthread_print_override_modularize is now failing on deno with a missing hello world message.

@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented May 29, 2026

Interestingly test_pthread_print_override_modularize is now failing on deno with a missing hello world message.

Is it just flaky?

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