emscripten-core / emscripten-core/emscripten

Deadlock on exit if secondary threads are writing to stdout

Open
#15,186 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

When using `PROXY_TO_PTHREAD` and `main()` returns (or if a secondary pthread calls `exit()`),
the `exitOnMainThread` function is used to proxy the application exit back top the main thread and
bring down the entire application.

This means that the `exit()` call ends up happens during `emscripten_current_thread_process_queued_calls`.
However, if there are threads that are printing to stdout it will then be blocked because writing
to stdout also requires a proxied operation which requires the main thread to process queued calls
but `emscripten_current_thread_process_queued_calls` does not recursion.

This means that when the main thread calls fflush(0), and it tried to `__lockfile(2)` there is
a deadlock because the thread trying to write to stdout is already holding this lock.

This issue is responsible for the flakiness in `test_pthread_c11_threads`: #14579

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.