Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 src/cpp/luv/HxcppLuv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void cpp::luv::stopLoop(uv_loop_t* loop)

void cpp::luv::shutdownLoop(uv_loop_t* loop)
{
hx::AutoGCFreeZone blocking;

uv_walk(loop, doWalkLoop, nullptr);
uv_run(loop, UV_RUN_DEFAULT);
}
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/luv/HxcppLuvAsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ void cpp::luv::async::close(uv_async_t* async)
uv_close(reinterpret_cast<uv_handle_t*>(async), [](uv_handle_t* handle) {
auto data = static_cast<hx::Object**>(handle->data);

// Removing the root touches GC memory, which must not happen from a GC free zone.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this comment is right (or the wording is very vague at least), seems like the core issue is that the root set mutex isn't acquired before the GC marks and visits the roots. I'm not sure if that was a conscious decision, but it sounds like it should acquire it, I can't immediately think of any reason why non GC threads shouldn't be able to create and remove roots.

@Simn Simn Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the comment was wrong. GCAddRoot/GCRemoveRoot take sGCRootLock, but MarkAll and the visit path iterate sgRootSet/sgOffsetRootSet without it. Leaving the free zone only worked around that by waiting out any running collection. I've opened HaxeFoundation/hxcpp#1438, which takes the lock while iterating, and dropped the root changes here, so this PR is now only the shutdownLoop free zone fix.

🤖 Generated with Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we still need humans after all. At least for now...

auto left = hx::TryExitGCFreeZone();
hx::GCRemoveRoot(data);
if (left) {
hx::EnterGCFreeZone();
}

delete data;
delete handle;
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/luv/HxcppLuvTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ int cpp::luv::timer::stop(uv_timer_t* timer)
void cpp::luv::timer::close(uv_timer_t* timer)
{
uv_close(reinterpret_cast<uv_handle_t*>(timer), [](uv_handle_t* handle) {
// Removing the root touches GC memory, which must not happen from a GC free zone.
auto left = hx::TryExitGCFreeZone();
delete static_cast<cpp::utils::RootedObject*>(handle->data);
if (left) {
hx::EnterGCFreeZone();
}
delete handle;
});
}
12 changes: 8 additions & 4 deletions src/cpp/luv/HxcppLuvWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ void onCallback(uv_work_t* work)
auto top = 0;
hx::SetTopOfStack(&top, true);

auto root = std::unique_ptr<cpp::utils::RootedObject>(static_cast<cpp::utils::RootedObject*>(work->data));
auto task = Dynamic(root->getObject());

task();
{
// The root has to be removed while this thread is still registered: the GC iterates
// the root set without locking, and only stops registered threads while doing so.
auto root = std::unique_ptr<cpp::utils::RootedObject>(static_cast<cpp::utils::RootedObject*>(work->data));
auto task = Dynamic(root->getObject());

task();
}

hx::UnregisterCurrentThread();
}
Expand Down