Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
libhxcpp 6.1.0 (unreleased)
------------------------------------------------------------

* Fixed the process ending with std::terminate where the platform cannot detach a thread, as on Switch with devkitA64, whose pthread_detach returns ENOSYS: std::thread::detach threw and the std::thread, still joinable, was destroyed, at the first Thread.create or when the collector, the file closer, the process reaper, the profiler or telemetry started a thread. The thread is kept and runs to its end as it would detached, and the runtime says once on stderr that finished threads will not be reclaimed, which only the platform can do (libhxcpp #502)

libhxcpp 6.0.4
------------------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion src/hx/Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <mutex>
#include <thread>
#include <chrono>
#include "thread/Detach.hpp"


#ifdef HX_WINRT
Expand Down Expand Up @@ -51,7 +52,7 @@ class Profiler

if (gThreadRefCount == 1) {
std::thread thread(ProfileMainLoop);
thread.detach();
hx::DetachThread(thread);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/hx/Telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mutex>
#include <thread>
#include <chrono>
#include "thread/Detach.hpp"

namespace hx
{
Expand Down Expand Up @@ -70,7 +71,7 @@ class Telemetry
if (gThreadRefCount == 1) {
std::thread thread(ProfileMainLoop);

thread.detach();
hx::DetachThread(thread);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <hx/thread/Thread.hpp>
#include "../Hash.h"
#include "../FastMutex.h"
#include "../thread/Detach.hpp"
#include "GcRegCapture.h"
#include <unordered_set>
#include <unordered_map>
Expand Down Expand Up @@ -4316,7 +4317,7 @@ class GlobalAllocator
try
{
std::thread thread(SReleaseLoop, this);
thread.detach();
hx::DetachThread(thread);
mReleaseStarted = true;
}
catch(...)
Expand Down Expand Up @@ -5739,7 +5740,7 @@ class GlobalAllocator

std::thread thread(SThreadLoop, info);

thread.detach();
hx::DetachThread(thread);
#endif
}

Expand Down
4 changes: 3 additions & 1 deletion src/hx/libs/std/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <thread>
#include <mutex>
#include <condition_variable>
#include "../../thread/Detach.hpp"
#endif

#include <hx/OS.h>
Expand Down Expand Up @@ -109,7 +110,8 @@ struct LateCloser
{
try
{
std::thread([this]() { run(); }).detach();
std::thread thread([this]() { run(); });
hx::DetachThread(thread);
}
catch(...)
{
Expand Down
4 changes: 3 additions & 1 deletion src/hx/libs/std/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#if !defined(HX_WINRT) && !defined(EPPC) && !defined(NEKO_WINDOWS) && !defined(__EMSCRIPTEN__) && !defined(HXCPP_SINGLE_THREADED_APP)
#define HX_PROCESS_REAPER
#include <thread>
#include "../../thread/Detach.hpp"
#include <mutex>
#include <chrono>
#endif
Expand Down Expand Up @@ -174,7 +175,8 @@ namespace
{
try
{
std::thread(reaperLoop).detach();
std::thread thread(reaperLoop);
hx::DetachThread(thread);
sReaper.running = true;
}
catch(...)
Expand Down
32 changes: 32 additions & 0 deletions src/hx/thread/Detach.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <thread>
#include <atomic>
#include <exception>
#include <utility>
#include <stdio.h>

namespace hx
{
// Detaches a std::thread the runtime will never join. Where the platform
// cannot detach, as devkitA64's newlib on Switch, whose pthread_detach
// returns ENOSYS, std::thread::detach throws, and the joinable std::thread
// then ended the process with std::terminate when it was destroyed, even
// inside a catch. The thread is kept instead: it runs to its end as it
// would detached, and what the platform holds for it is not reclaimed.
inline void DetachThread(std::thread &ioThread)
{
try
{
ioThread.detach();
}
catch (const std::exception &e)
{
static std::atomic<bool> sReported(false);
if (!sReported.exchange(true))
fprintf(stderr, "Could not detach a thread (%s): finished threads will not be reclaimed\n", e.what());
// Never destroyed, as destroying a joinable std::thread terminates
new std::thread(std::move(ioThread));
}
}
}
5 changes: 3 additions & 2 deletions src/hx/thread/ThreadImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <system_error>
#include <hx/thread/CountingSemaphore.hpp>
#include "ThreadImpl.hpp"
#include "Detach.hpp"

namespace
{
Expand Down Expand Up @@ -206,7 +207,7 @@ hx::thread::ThreadImpl_obj::Native::Native(Thread_obj::CreateFunction _job, Thre
// This does feel like a bit of a bodge and something which will actually need to be addressed to add name setting on Linux.

#ifndef HX_WINDOWS
thread.detach();
hx::DetachThread(thread);
#endif
}

Expand Down Expand Up @@ -269,7 +270,7 @@ void hx::thread::ThreadImpl_obj::finalise(hx::Object* obj)

if (native->thread.joinable())
{
native->thread.detach();
hx::DetachThread(native->thread);
}
}

Expand Down
81 changes: 81 additions & 0 deletions test/regression/ThreadDetachFailure/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import sys.thread.Lock;
import sys.thread.Thread;

// Threads where the platform cannot detach one. devkitA64's newlib on Switch
// has no pthread_detach hook, so it returns ENOSYS, std::thread::detach
// throws, and the joinable std::thread being destroyed ended the process
// with std::terminate at the first Thread.create, or when the collector
// started a worker. On glibc this program replaces pthread_detach with one
// that does the same. Elsewhere it cannot, and prints what is expected.
@:cppFileCode('
#if defined(__linux__)
#include <pthread.h>
#include <errno.h>
#endif
#include <atomic>

#if defined(__linux__) && defined(__GLIBC__)
static std::atomic<int> refusedDetaches(0);

extern "C" __attribute__((visibility("default"))) int pthread_detach(pthread_t) __THROW
{
refusedDetaches++;
return ENOSYS;
}

static bool detachRefused() { return refusedDetaches > 0; }
#else
static bool detachRefused() { return true; }
#endif

// Under ThreadSanitizer, which this pthread_detach bypasses, every thread
// the runtime kept looks like a leak at exit, as none was detached or joined.
// That is what this tests, so leaks are not reported here; anything else
// still fails the run. TSAN_OPTIONS, which the CI job sets, adds to these.
#if defined(__SANITIZE_THREAD__)
#define THREAD_DETACH_TSAN 1
#elif defined(__has_feature)
#if __has_feature(thread_sanitizer)
#define THREAD_DETACH_TSAN 1
#endif
#endif
#ifdef THREAD_DETACH_TSAN
extern "C" __attribute__((visibility("default"), used)) const char *__tsan_default_options()
{
return "report_thread_leaks=0";
}
#endif
')
class Main {
static function main() {
final count = 32;
final done = new Lock();
var ran = 0;
final mutex = new sys.thread.Mutex();
for (i in 0...count) {
Thread.create(() -> {
// Allocates enough for collections to run while threads come and go
var keep = [];
for (j in 0...2000) {
keep.push([j, i]);
}
cpp.vm.Gc.run(false);
mutex.acquire();
ran++;
mutex.release();
done.release();
});
}
var all = true;
for (i in 0...count) {
if (!done.wait(30)) {
all = false;
break;
}
}
cpp.vm.Gc.run(true);
final refused:Bool = untyped __cpp__("detachRefused()");
Sys.println("threads created after a refused detach run to their end: " + (all && ran == count));
Sys.println("the platform refused to detach them: " + refused);
}
}
2 changes: 2 additions & 0 deletions test/regression/ThreadDetachFailure/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
2 changes: 2 additions & 0 deletions test/regression/ThreadDetachFailure/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
threads created after a refused detach run to their end: true
the platform refused to detach them: true
1 change: 1 addition & 0 deletions test/regression/emscripten-skip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ SslNativeMemoryPressure: threads (std::thread, which needs a pthreads build)
SslPeerCertificateLifetime: sockets
SslSniLifetime: sockets
SslSniName: sockets
ThreadDetachFailure: threads
ThreadEndsAtExit: threads
ThreadEndsAtExitDebugger: threads
ThreadEndsWhileCollecting: threads
Expand Down
1 change: 1 addition & 0 deletions toolchain/haxe-target-threads.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<xml>
<depend name="${HXCPP}/src/hx/thread/ThreadImpl.hpp"/>
<depend name="${HXCPP}/src/hx/thread/Detach.hpp"/>

<file name="src/hx/thread/RecursiveMutex.cpp"/>
<file name="src/hx/thread/ConditionVariable.cpp"/>
Expand Down
1 change: 1 addition & 0 deletions toolchain/haxe-target.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<depend name="${HXCPP}/src/hx/gc/Incremental.h"/>
<depend name="${HXCPP}/src/hx/gc/IncrementalApi.h"/>
<depend name="${HXCPP}/src/hx/thread/ThreadImpl.hpp"/>
<depend name="${HXCPP}/src/hx/thread/Detach.hpp"/>
<depend name="${HXCPP}/include/hx/DynamicImpl.h"/>
<depend name="${HXCPP}/include/hx/Memory.h"/>
<depend name="${HXCPP}/include/hx/NekoFunc.h"/>
Expand Down
Loading