From 60ef3aaba74ca72e04a42551a3769746cf298c9d Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 18:34:36 +0000 Subject: [PATCH 1/2] Keep the GC root set alive until the process ends hx::thread::Thread_obj::current() gives every thread that it is called on, including foreign threads, a thread-local ThreadInfoHolder whose destructor calls hx::GCRemoveRoot. Threads that are still running when the process exits, such as the libuv thread pool, which libuv only joins from a destructor function, run that destructor after the static objects have been destroyed. GCRemoveRoot then erased from the destroyed static root set, which corrupted the heap or crashed the process after main had returned. Allocate the root set on first use and never free it, like the offset root set next to it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn --- src/hx/gc/Immix.cpp | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 4ee770077..bfdcd6a97 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -2372,7 +2372,11 @@ void MarkStringArray(String *inPtr, int inLength, hx::MarkContext *__inCtx) FILE_SCOPE std::mutex* sGCRootLock = nullptr; typedef std::unordered_set RootSet; -static RootSet sgRootSet; +// Allocated on first use and never freed. Threads that are still running when the process exits, +// such as the thread pools of native libraries, remove their roots from thread-local destructors +// (see ThreadInfoHolder), which can run after the destructors of static objects. A static set +// would already be destroyed by then. +static RootSet *sgRootSet=0; typedef std::unordered_map OffsetRootSet; static OffsetRootSet *sgOffsetRootSet=0; @@ -2380,13 +2384,16 @@ static OffsetRootSet *sgOffsetRootSet=0; void GCAddRoot(hx::Object **inRoot) { std::lock_guard lock(*sGCRootLock); - sgRootSet.insert(inRoot); + if (!sgRootSet) + sgRootSet = new RootSet(); + sgRootSet->insert(inRoot); } void GCRemoveRoot(hx::Object **inRoot) { std::lock_guard lock(*sGCRootLock); - sgRootSet.erase(inRoot); + if (sgRootSet) + sgRootSet->erase(inRoot); } @@ -4270,12 +4277,13 @@ class GlobalAllocator // Threads in GC free zones, or not registered at all, can still add and remove roots std::lock_guard rootLock(*hx::sGCRootLock); - for(hx::RootSet::iterator i = hx::sgRootSet.begin(); i!=hx::sgRootSet.end(); ++i) - { - hx::Object **obj = *i; - if (*obj) - inCtx->visitObject(obj); - } + if (hx::sgRootSet) + for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) + { + hx::Object **obj = *i; + if (*obj) + inCtx->visitObject(obj); + } if (hx::sgOffsetRootSet) @@ -4720,12 +4728,13 @@ class GlobalAllocator // Threads in GC free zones, or not registered at all, can still add and remove roots std::lock_guard rootLock(*hx::sGCRootLock); - for(hx::RootSet::iterator i = hx::sgRootSet.begin(); i!=hx::sgRootSet.end(); ++i) - { - hx::Object *&obj = **i; - if (obj) - hx::MarkObjectAlloc(obj , &mMarker ); - } + if (hx::sgRootSet) + for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) + { + hx::Object *&obj = **i; + if (obj) + hx::MarkObjectAlloc(obj , &mMarker ); + } if (hx::sgOffsetRootSet) for(hx::OffsetRootSet::iterator i = hx::sgOffsetRootSet->begin(); i!=hx::sgOffsetRootSet->end(); ++i) From d7efc3bebf8b1e9915e20939045430104654bb23 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 18:56:00 +0000 Subject: [PATCH 2/2] Allocate the GC root set eagerly The root lock is only created in InitAlloc, so no root can be added before the set exists, and the null checks are unnecessary. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn --- src/hx/gc/Immix.cpp | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index bfdcd6a97..367b2760a 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -2372,11 +2372,10 @@ void MarkStringArray(String *inPtr, int inLength, hx::MarkContext *__inCtx) FILE_SCOPE std::mutex* sGCRootLock = nullptr; typedef std::unordered_set RootSet; -// Allocated on first use and never freed. Threads that are still running when the process exits, -// such as the thread pools of native libraries, remove their roots from thread-local destructors -// (see ThreadInfoHolder), which can run after the destructors of static objects. A static set -// would already be destroyed by then. -static RootSet *sgRootSet=0; +// Never freed. Threads that are still running when the process exits, such as the thread pools of +// native libraries, remove their roots from thread-local destructors (see ThreadInfoHolder), which +// can run after the destructors of static objects. A static set would already be destroyed by then. +static RootSet *sgRootSet = new RootSet(); typedef std::unordered_map OffsetRootSet; static OffsetRootSet *sgOffsetRootSet=0; @@ -2384,16 +2383,13 @@ static OffsetRootSet *sgOffsetRootSet=0; void GCAddRoot(hx::Object **inRoot) { std::lock_guard lock(*sGCRootLock); - if (!sgRootSet) - sgRootSet = new RootSet(); sgRootSet->insert(inRoot); } void GCRemoveRoot(hx::Object **inRoot) { std::lock_guard lock(*sGCRootLock); - if (sgRootSet) - sgRootSet->erase(inRoot); + sgRootSet->erase(inRoot); } @@ -4277,13 +4273,12 @@ class GlobalAllocator // Threads in GC free zones, or not registered at all, can still add and remove roots std::lock_guard rootLock(*hx::sGCRootLock); - if (hx::sgRootSet) - for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) - { - hx::Object **obj = *i; - if (*obj) - inCtx->visitObject(obj); - } + for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) + { + hx::Object **obj = *i; + if (*obj) + inCtx->visitObject(obj); + } if (hx::sgOffsetRootSet) @@ -4728,13 +4723,12 @@ class GlobalAllocator // Threads in GC free zones, or not registered at all, can still add and remove roots std::lock_guard rootLock(*hx::sGCRootLock); - if (hx::sgRootSet) - for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) - { - hx::Object *&obj = **i; - if (obj) - hx::MarkObjectAlloc(obj , &mMarker ); - } + for(hx::RootSet::iterator i = hx::sgRootSet->begin(); i!=hx::sgRootSet->end(); ++i) + { + hx::Object *&obj = **i; + if (obj) + hx::MarkObjectAlloc(obj , &mMarker ); + } if (hx::sgOffsetRootSet) for(hx::OffsetRootSet::iterator i = hx::sgOffsetRootSet->begin(); i!=hx::sgOffsetRootSet->end(); ++i)