Skip to content

Keep the GC root set alive until the process ends - #1465

Merged
Simn merged 2 commits into
masterfrom
claude/awesome-ride-6o3lsd
Nov 4, 2026
Merged

Simn merged 2 commits into
masterfrom
claude/awesome-ride-6o3lsd

Conversation

@Simn

@Simn Simn commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Problem

hx::thread::Thread_obj::current() gives every thread it is called on, foreign threads included, a thread_local ThreadInfoHolder (src/hx/thread/ThreadImpl.cpp) whose destructor calls hx::GCRemoveRoot. That erases from sgRootSet, a static std::unordered_set in Immix.cpp.

Threads that are still alive when the process exits run that destructor after the static objects have been destroyed. libuv's thread pool is one: libuv only joins its workers from uv_library_shutdown, a __attribute__((destructor)) function that runs from _dl_fini, after exit() has run the static destructors. The workers then erase from the destroyed set. That is undefined behaviour, and it shows up as different symptoms.

This is the intermittent crash after all tests had passed in hxcoro's cpp CI (HaxeFoundation/hxcoro#219): double free or corruption (!prev), corrupted size vs. prev_size while consolidating, or, as in this core dump from a CI run that looped the test binary, a SIGSEGV that HXCPP_CATCH_SEGV turns into std::terminate:

Thread 1 (a libuv pool thread):
#13 hx::hxSignalFunction(int)
#14 <signal handler called>
#15 hx::GCRemoveRoot(hx::Object**)
#16 (anonymous namespace)::ThreadInfoHolder::~ThreadInfoHolder()
#17 __GI___call_tls_dtors ()
#18 start_thread

Thread 8 (main):
#6  uv_thread_join ()
#7  uv.threadpool_cleanup ()
#8  _dl_call_fini
#10 __run_exit_handlers
#12 __hxcpp_exit(int)

Locally it doesn't crash, but gdb shows the same order: the pool threads call GCRemoveRoot after the set's destructor has run. Its element count reads 0, and its bucket pointer is stale.

Change

Make sgRootSet a pointer to a set that is allocated in its static initializer and never freed. GCAddRoot already requires sGCRootLock, which InitAlloc creates, so no root can be added before the set exists, and the code needs no null checks. sGCRootLock is itself a pointer that is never freed, so the lock taken in GCRemoveRoot stays valid as well.

The comments below cover the other places in Immix and the debugger that could be reached after static destruction.

Testing

  • Built hxcoro's cpp test suite (debug) against this branch: 383/383 pass. In gdb during exit(), all four libuv pool threads now remove their roots from the live, heap-allocated set (same pointer, counts 6, 6, 6, 5) instead of freed memory.
  • A CI run that looped hxcoro's cpp test binaries confirmed that keeping libuv's pool threads away from the destroyed set removes the crash: 8192 runs with no crash, against 3 crashes in about 4 runs per runner before. That run used an hxcoro-side atexit workaround that will not be merged. This PR fixes the cause instead.
  • No regression test is included. The bug is a use-after-free during exit that usually goes unnoticed, so a test would only fail reliably under ASan or similar.
  • The (latest) / runtime tests failures come from a Haxe nightly that predates the revert of Fix unreflective haxe#12897 (see the comments below), not from this change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn

Simn commented Nov 4, 2026 •

Copy link
Copy Markdown
Member Author

Correction: HaxeFoundation/haxe#12897 was already reverted by HaxeFoundation/haxe#13143. However, the nightly that build.haxe.org serves as haxe_latest is still 886a9c7, from before the revert. It is still at that commit for the run on d7efc3b (Generated by Haxe 5.0.0-preview.1+886a9c7). So the (latest) / runtime tests jobs keep failing until the nightly is rebuilt, and re-running them before then won't help. The 4.3.7 jobs aren't affected.

The failure is a compile error from include/hx/Invoker.h:51 in the native step, on gcc, clang and MSVC. HaxeFoundation/haxe#12897 made the generator emit _dyn() closures for @:unreflective functions such as HaxeFunctions.resize_vec_by_ref, which takes a Reference<StdVector<T>>. The cause is that compiler change, not this PR: the same error reproduced on unmodified master with that nightly.


Generated by Claude Code

@Simn

Simn commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

HaxeFoundation/haxe#12897 was reverted by HaxeFoundation/haxe#13143, so this shouldn't be happening anymore.

Also, what's the reason for not always initializing sgRootSet right away and avoiding all these ifs?

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Va4FXDtgRomWVWe99UmFhn

Simn commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

HaxeFoundation/haxe#12897 was reverted by HaxeFoundation/haxe#13143, so this shouldn't be happening anymore.

Right. Those jobs built with Haxe 886a9c7, which is older than the revert (badcb0a), so they used a nightly from before it landed. A run with a current nightly shouldn't hit this. I've corrected my earlier comment.

Also, what's the reason for not always initializing sgRootSet right away and avoiding all these ifs?

There isn't a good one. GCAddRoot already dereferences sGCRootLock, which is only created in InitAlloc, so no root can be added before the GC is set up. d7efc3b allocates the set in its static initializer and removes the null checks. The diff against master is now just sgRootSet. → sgRootSet->.


Generated by Claude Code

@Simn

Simn commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

I'm not much of a C++ person but this makes sense to me and looks safe.

While we're here, did you check Immix for other occurrences of this pattern?

Simn commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

While we're here, did you check Immix for other occurrences of this pattern?

Yes. Code can reach GC state after the static destructors have run in two ways:

1. Thread-local destructors. ThreadInfoHolder is the only thread_local with a destructor in hxcpp. hx::TLSData creates its pthread key without a destructor, so it runs nothing at thread exit. ~ThreadInfoHolder only calls GCRemoveRoot and deletes a heap pointer, so this PR covers this path completely. This is the path libuv's idle pool threads take, and the one behind the hxcoro crash.

2. A Haxe thread that finishes its job during exit and calls hx::UnregisterCurrentThread → LocalAllocator::Release. This touches:

  • sGlobalAlloc and gThreadStateChangeLock: heap-allocated and never freed, so they're fine.
  • sGlobalChunks (generational GC): a static, but it has only raw pointers and ints and no destructor, so it's fine.
  • onThreadDetach → with HXCPP_DEBUGGER, DebuggerContext::detach() erases from the static std::list gList and std::map gMap in Debugger.cpp. That's the same pattern. After that it also calls the Haxe g_eventNotificationHandler, which means running Haxe code during exit. This only happens in debugger builds, and only when a thread finishes its job at the same moment the process exits.

The remaining FILE_SCOPE containers in Immix (sWeakRefs, sFinalizerMap, sHaxeFinalizerMap, sObjectIdMap, sMakeZombieSet, sZombieList, sWeakHashList, sFinalizableList, sBlockPool, ...) are only used when allocating weak refs, setting finalizers, __hxcpp_obj_id, and during collections. A thread can only reach them after static destruction if it is still actively running Haxe code, e.g. allocating and triggering a collection, while main exits. That's a broader "threads still running at exit" problem. Never freeing a few containers wouldn't make it safe, so I've left those alone.

So the root set is the only instance reachable from an idle thread. The debugger's gList/gMap is the one other spot with the same shape. I can make those heap-allocated too, here or in a separate PR, if you want.


Generated by Claude Code

@Simn

Simn commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

I'll merge this because the hxcoro CI is ruining my day right now, and that way I can see if it actually helps and solves the crashes I've been seeing.

@Simn
Simn merged commit e0e0cdc into master Nov 4, 2026
53 of 63 checks passed
@Simn
Simn deleted the claude/awesome-ride-6o3lsd branch November 4, 2026 20:11
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.

2 participants