Repository navigation
Reset LocalAllocator when returning it to the pool - #1435
Conversation
Between GetPooledAllocator and AddLocal, the allocator is neither pooled nor registered, so a collection in that window does not reset it. The thread then allocates from holes that the collection may have handed out again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Part of me wonders if a local allocator should be reset when pulled from the pool |
Pooled allocators now never hold holes, instead of resetting them when a thread attaches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Moved the reset into I didn't remove the Tested with hxcoro's C++ tests from HaxeFoundation/hxcoro#164, which runs the suite on 🤖 Generated with Claude Code |
|
I don't fully understand what this whole "hole" business is about, this this fix (still) does help with my access violation problems. |
|
IIUC when an allocation is moved / collected from a block a "hole" is recorded in that block where the object previously lived. Future allocations into that block will check for a hole large enough to reuse before resorting to bump allocating at the end of the block / getting a fresh block. |
|
Right, makes sense. That makes me wonder why this isn't a more common issue, but I suppose there's a timing component here with the allocator being in an intermittently bad state. |
|
Aidan's description of the outcome is right: the allocator ends up allocating over live objects. Two details of the mechanism are different, though. Holes aren't recorded when objects are moved or collected. Immix divides each block into lines and marks the lines that contain live objects during a collection. At the end of the collection, each block's holes are recomputed from those marks as runs of unmarked lines ( The allocator keeps its old block, not a new block with old holes. A
Resetting at the end of each collection exists to prevent exactly this. Every registered allocator drops its window there, and only blocks that nobody holds go back to the free list. The allocator caught in the window misses that reset. With this PR, pooled allocators never hold a window in the first place. Why this is rare: all of these have to coincide:
Most programs attach a thread once, so they almost never hit the window. hxcpp_luv_io's 🤖 Generated with Claude Code |
- Make the shutdown check atomic: numPending switches from 0 to -1, and dispatch increments it with a CAS that fails once it is negative. - Remove Luv.shutdown() from the test runner. The crashes were caused by HaxeFoundation/hxcpp#1435, not by libuv's library destructor. - Use a markdown list in the createDefault doc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* try C++ LuvDispatcher again * Fix LuvDispatcher loop shutdown crash (uv__queue_done assertion) and post-exit ThreadInfoHolder segfault (#165) * ... * Initial plan * Fix LuvDispatcher crash: use shutdownLoop instead of stopLoop before freeLoop The Ubuntu CI job failed with: uv__queue_done: Assertion `uv__has_active_reqs(req->loop)' failed. Root cause: the main thread freed the libuv loop (via freeLoop) while a worker thread was still executing uv__queue_done cleanup after its work callback. The old stopLoop/freeLoop sequence left async close callbacks and work-completion callbacks unprocessed. Fix: replace stopLoop(loop) with shutdownLoop(loop), which calls uv_walk (closes any remaining handles) then uv_run(UV_RUN_DEFAULT) to drain all pending callbacks (onAfter for work items, close callbacks for async handles) before the loop is freed. Calling stopLoop would set stop_flag=1, causing uv_run in shutdownLoop to exit immediately without processing callbacks, so it must be removed. Co-authored-by: Simn <634365+Simn@users.noreply.github.com> * Fix LuvDispatcher shutdown: use drainLoop (no uv_walk, no GCFreeZone) and blocking pool shutdown Co-authored-by: Simn <634365+Simn@users.noreply.github.com> * Revert Setup.hx to use shutdownLoop; keep pool.shutDown(true) fix Co-authored-by: Simn <634365+Simn@users.noreply.github.com> * install gdb * sudo * Fix LuvDispatcher crash: run work directly on loop thread, not via Work.queue Co-authored-by: Simn <634365+Simn@users.noreply.github.com> * Revert "Fix LuvDispatcher crash: run work directly on loop thread, not via Work.queue" This reverts commit 6178fd7. * manually shutdown libuv library on test completion --------- Co-authored-by: Simon Krajewski <simon@haxe.org> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Simn <634365+Simn@users.noreply.github.com> Co-authored-by: Aidan Lee <aidan.lee@evcam.com> * Drain LuvDispatcher before closing its handle Work that is still running when a setup is closed can dispatch more work, e.g. a channel continuation that resumes its own continuation asynchronously. That sent on the already closed async handle, and the work was dropped. shutDown now runs the loop until all dispatched objects have finished, and dispatching after that throws, like FixedThreadPool does. The dispatcher is also shut down before the scheduler, because running work might still use it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Address review comments on LuvDispatcher shutdown - Make the shutdown check atomic: numPending switches from 0 to -1, and dispatch increments it with a CAS that fails once it is negative. - Remove Luv.shutdown() from the test runner. The crashes were caused by HaxeFoundation/hxcpp#1435, not by libuv's library destructor. - Use a markdown list in the createDefault doc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: Simn <634365+Simn@users.noreply.github.com> Co-authored-by: Aidan Lee <aidan.lee@evcam.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
When a thread registers,
RegisterCurrentThreadtakes aLocalAllocatorfrom the pool inGetPooledAllocator, which holdsgThreadStateChangeLockonly for that step.AttachThreadthen callsAddLocal, which takes the lock again. In between, the allocator is neither in the pool nor inmLocalAllocs.A collection resets every registered allocator at the end of
Markand every pooled one inClearPooledAlloc. An allocator caught in that window gets neither reset. It keeps its holes, the collection may reclaim those blocks for other allocations, and the newly attached thread then allocates over live objects.The fix calls
Reset()at the start ofAttachThread. That only drops the allocator's current holes, so the thread starts with a fresh allocation region.This is easy to hit when threads attach and detach very often. hxcpp_luv_io's
Work.queueregisters the libuv worker thread withSetTopOfStack(&top, true)and unregisters it withUnregisterCurrentThread()for every work item. hxcoro'sLuvDispatchersends every dispatch throughWork.queue. With that dispatcher, about 25% of hxcoro's C++ test runs on Windows crashed with access violations in unrelated objects (6 of 30). With this change, there were no access violations in 60 runs.🤖 Generated with Claude Code