Skip to content

Keep a thread the platform cannot detach instead of terminating - #503

Open
dimensionscape wants to merge 1 commit into
mainfrom
fix/thread-detach-failure
Open

dimensionscape wants to merge 1 commit into
mainfrom
fix/thread-detach-failure

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

Where the platform cannot detach a thread (Switch with devkitA64, whose pthread_detach returns ENOSYS), every place the runtime detached a std::thread ended the process with std::terminate. They all go through hx::DetachThread now, which keeps such a thread for the life of the process instead, so it runs to its end as it would detached, and says once on stderr that finished threads will not be reclaimed.

ThreadDetachFailure replaces pthread_detach on glibc with one that returns ENOSYS. On other platforms it cannot, and prints what is expected.

Fixes #502

Where pthread_detach fails, as on Switch with devkitA64, whose newlib
returns ENOSYS because libnx installs no __syscall_thread_detach hook,
std::thread::detach throws std::system_error. Every place the runtime
detached a thread then destroyed a std::thread that was still joinable,
which is std::terminate: ThreadImpl's Native and its finaliser, the
collector's workers and release thread, the late file closer, the
process reaper, and the profiler and telemetry threads. The catch(...)
around three of them never ran, as the unwind destroyed the thread first.

They all detach through hx::DetachThread, in src/hx/thread/Detach.hpp.
When detach throws, it moves the std::thread to the heap and never
destroys it, so the thread runs to its end as it would detached, and
says once on stderr that finished threads will not be reclaimed. That
is left to the platform, which alone can free what it holds for them.

ThreadDetachFailure replaces pthread_detach on glibc with one that
returns ENOSYS, creates threads that allocate and collect, and checks
that they all ran and that the detach was refused.

Tests: ThreadDetachFailure

Fixes #502
@dimensionscape
dimensionscape force-pushed the fix/thread-detach-failure branch from 01fdc9a to efdf7b4 Compare November 28, 2026 07:40
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.

A thread the platform cannot detach ends the process with std::terminate

1 participant