Repository navigation
Keep a thread the platform cannot detach instead of terminating - #503
Open
dimensionscape wants to merge 1 commit into
Open
dimensionscape wants to merge 1 commit into
dimensionscape wants to merge 1 commit into
Conversation
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
force-pushed
the
fix/thread-detach-failure
branch
from
November 28, 2026 07:40
01fdc9a to
efdf7b4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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