Skip to content

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

In review

Description

@dimensionscape

Where the platform cannot detach a thread, the runtime ends the process with std::terminate.

devkitA64's newlib on Switch implements pthread_detach through a libnx hook, __syscall_thread_detach, that libnx does not install (it is commented out as unsupported in nx/source/runtime/newlib.c), so pthread_detach returns ENOSYS. std::thread::detach then throws std::system_error, and the std::thread being destroyed is still joinable, which is std::terminate. That happens at the first Thread.create, and wherever else the runtime starts a thread it detaches:

  • ThreadImpl_obj::Native's constructor and ThreadImpl_obj::finalise (src/hx/thread/ThreadImpl.cpp)
  • the collector's worker threads and its release thread (src/hx/gc/Immix.cpp)
  • the late file closer (src/hx/libs/std/File.cpp) and the process reaper (src/hx/libs/std/Process.cpp)
  • the profiler and telemetry threads

The file closer, the process reaper and the release thread put the detach in a catch(...), but that never runs for a failed detach: the unwind destroys the joinable std::thread first.

A program can install a hook that returns 0 and does nothing, which stops the abort, but libnx frees a thread only in pthread_join (threadClose and the control block), so every finished thread then leaks its kernel handle and stack until no more can be created.

The runtime cannot reclaim threads on a platform that has no detach, and that belongs to the platform's runtime. It should not end the process, though: when detach fails, the thread should be kept and left to run to its end, as it would detached.

Activity

  1. added a commit that references this issue on Nov 28, 2026
    efdf7b4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions