From 05720a6898761f2029d66f6a1b18d2cc8f7e1091 Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Fri, 27 Feb 2026 22:48:49 +0100 Subject: [PATCH 1/4] try C++ LuvDispatcher again --- src/hxcoro/run/Setup.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hxcoro/run/Setup.hx b/src/hxcoro/run/Setup.hx index 33b08e9a..fd6c5c09 100644 --- a/src/hxcoro/run/Setup.hx +++ b/src/hxcoro/run/Setup.hx @@ -122,7 +122,7 @@ class Setup { #if interp return createLuv(); #elseif (cpp && hxcpp_luv_io) - return createLuvThreadPool(); + return createLuv(); #elseif (jvm || cpp || hl) return createThreadPool(10); #else From 6646e984d093b8aa0a0250d5a1b000acb8629d14 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Mar 2026 13:16:38 +0100 Subject: [PATCH 2/4] 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 6178fd790066263d05ff35d191a0377b2010b30e. * manually shutdown libuv library on test completion --------- Co-authored-by: Simon Krajewski 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 --- .github/workflows/copilot-setup-steps.yml | 4 ++++ README.md | 2 +- src/hxcoro/run/Setup.hx | 2 +- tests/src/Main.hx | 8 +++++++- tests/src/run/TestEntrypoints.hx | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/.github/workflows/copilot-setup-steps.yml b/.github/workflows/copilot-setup-steps.yml index b73254ab..5f976c92 100644 --- a/.github/workflows/copilot-setup-steps.yml +++ b/.github/workflows/copilot-setup-steps.yml @@ -35,3 +35,7 @@ jobs: uses: HaxeFoundation/setup-hashlink@v1 with: version: nightly + + - name: Setup system + run: | + sudo apt-get install -y gdb diff --git a/README.md b/README.md index d7183c12..71541857 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ class Main { ## Capabilities -- **Asynchronous clals** — write asynchronous code in a straightforward sequential style using `@:coroutine` methods. +- **Asynchronous calls** — write asynchronous code in a straightforward sequential style using `@:coroutine` methods. - **Structured concurrency** — manage lightweight groups of child tasks with well-defined lifetimes and cancellation semantics. - **Generators** — produce lazy sequences with `HaxeGenerator`, `Es6Generator`, `CsGenerator`, and the async variant `AsyncGenerator`. - **Cancellation** — first-class `CancellationException` support throughout, with `suspendCancellable` for registering cleanup callbacks. diff --git a/src/hxcoro/run/Setup.hx b/src/hxcoro/run/Setup.hx index fd6c5c09..161e94be 100644 --- a/src/hxcoro/run/Setup.hx +++ b/src/hxcoro/run/Setup.hx @@ -65,7 +65,7 @@ class Setup { function finalize() { scheduler.shutDown(); shutDownDispatcher(dispatcher); - cpp.luv.Luv.stopLoop(loop); + cpp.luv.Luv.shutdownLoop(loop); cpp.luv.Luv.freeLoop(loop); } return new LoopSetup(scheduler, dispatcher, finalize); diff --git a/tests/src/Main.hx b/tests/src/Main.hx index 324769f3..397b0e3c 100644 --- a/tests/src/Main.hx +++ b/tests/src/Main.hx @@ -45,6 +45,12 @@ function main() { runner.addCases("schedulers"); runner.addCases("structured"); - utest.ui.Report.create(runner, NeverShowSuccessResults, AlwaysShowHeader); + final reporter = utest.ui.Report.create(runner, NeverShowSuccessResults, AlwaysShowHeader); +#if (cpp && hxcpp_luv_io) + reporter.setHandler(_ -> { + cpp.luv.Luv.shutdown(); + }); +#end + runner.run(); } \ No newline at end of file diff --git a/tests/src/run/TestEntrypoints.hx b/tests/src/run/TestEntrypoints.hx index e7dd7b9d..e83d0d72 100644 --- a/tests/src/run/TestEntrypoints.hx +++ b/tests/src/run/TestEntrypoints.hx @@ -159,7 +159,7 @@ class TestEntrypoints extends utest.Test { final setup = Setup.createLuvGen( (uvLoop, loop) -> new ThreadPoolDispatcher(loop, pool), dispatcher -> { - pool.shutDown(); + pool.shutDown(true); } ); final context = setup.createContext(); From 65e06c6eed9fead14b8964bb8593400529c0f7d8 Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Wed, 28 Oct 2026 21:41:10 +0100 Subject: [PATCH 3/4] 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 --- src/hxcoro/dispatchers/LuvDispatcher.cpp.hx | 43 ++++++++++++++++----- src/hxcoro/run/Setup.hx | 3 +- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx b/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx index ba936099..c50a73a5 100644 --- a/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx +++ b/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx @@ -2,6 +2,9 @@ package hxcoro.dispatchers; import cpp.luv.Luv; import cpp.luv.Work; +import haxe.Exception; +import haxe.atomic.AtomicBool; +import haxe.atomic.AtomicInt; import haxe.coro.dispatchers.Dispatcher; import haxe.coro.dispatchers.IDispatchObject; import haxe.coro.schedulers.IScheduler; @@ -17,10 +20,13 @@ import hxcoro.schedulers.LuvScheduler; class LuvDispatcher extends Dispatcher { final loop:LuvLoop; - final workQueue:AsyncDeque<()->Void>; + final workQueue:AsyncDeque; final s:IScheduler; // Only set if we create it final luvScheduler:Option; + // Objects that were dispatched but have not finished running yet + final numPending:AtomicInt; + final isShutDown:AtomicBool; function get_scheduler():IScheduler { return s; @@ -28,6 +34,8 @@ class LuvDispatcher extends Dispatcher public function new(loop:LuvLoop, ?scheduler:IScheduler) { this.loop = loop; + numPending = new AtomicInt(0); + isShutDown = new AtomicBool(false); workQueue = new AsyncDeque(loop, @:nullSafety(Off) loopWork); if (scheduler == null) { @@ -40,27 +48,44 @@ class LuvDispatcher extends Dispatcher } } + /** + Runs `obj` on the libuv thread pool. Throws if this dispatcher has been shut down. + **/ public function dispatch(obj:IDispatchObject) { - workQueue.add(obj.onDispatch); + if (isShutDown.load()) { + throw new Exception("LuvDispatcher has been shut down"); + } + numPending.add(1); + workQueue.add(obj); } function loopWork() { - consumeDeque(workQueue, event -> { - Work.queue(loop, event); + consumeDeque(workQueue, obj -> { + Work.queue(loop, () -> { + obj.onDispatch(); + numPending.sub(1); + }); }); } /** - Shuts down the scheduler if it is owned by this dispatcher, queues any pending work and - closes the internal libuv handle. + Runs `loop` until all dispatched objects have finished, including objects that are + dispatched while doing so. Then closes the internal libuv handle and shuts down the + scheduler if it is owned by this dispatcher. + + Must be called from the thread that runs `loop`. **/ public function shutDown() { + while (numPending.load() > 0) { + // Each finished work item wakes the loop up, so this does not miss the last one. + Luv.runLoop(loop, Once); + } + isShutDown.store(true); + workQueue.close(); switch (luvScheduler) { case Some(s): s.shutDown(); case None: } - loopWork(); - workQueue.close(); } -} \ No newline at end of file +} diff --git a/src/hxcoro/run/Setup.hx b/src/hxcoro/run/Setup.hx index cc885561..e23dcd6e 100644 --- a/src/hxcoro/run/Setup.hx +++ b/src/hxcoro/run/Setup.hx @@ -85,8 +85,9 @@ class Setup { final scheduler = new hxcoro.schedulers.LuvScheduler(loop); final dispatcher = createDispatcher(loop, scheduler); function finalize() { - scheduler.shutDown(); + // The dispatcher goes first: work that is still running might use the scheduler. shutDownDispatcher(dispatcher); + scheduler.shutDown(); cpp.luv.Luv.shutdownLoop(loop); cpp.luv.Luv.freeLoop(loop); } From a6e267354a1bbe84d52ba97d196f5a7299d306b1 Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Thu, 29 Oct 2026 16:41:54 +0100 Subject: [PATCH 4/4] 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 --- src/hxcoro/dispatchers/LuvDispatcher.cpp.hx | 35 ++++++++++++++------- src/hxcoro/run/Setup.hx | 6 ++-- tests/src/Main.hx | 5 --- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx b/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx index c50a73a5..b5ad753e 100644 --- a/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx +++ b/src/hxcoro/dispatchers/LuvDispatcher.cpp.hx @@ -3,12 +3,12 @@ package hxcoro.dispatchers; import cpp.luv.Luv; import cpp.luv.Work; import haxe.Exception; -import haxe.atomic.AtomicBool; import haxe.atomic.AtomicInt; import haxe.coro.dispatchers.Dispatcher; import haxe.coro.dispatchers.IDispatchObject; import haxe.coro.schedulers.IScheduler; import haxe.ds.Option; +import hxcoro.concurrent.BackOff; import hxcoro.schedulers.LuvScheduler; /** @@ -24,9 +24,8 @@ class LuvDispatcher extends Dispatcher final s:IScheduler; // Only set if we create it final luvScheduler:Option; - // Objects that were dispatched but have not finished running yet + // Objects that were dispatched but have not finished running yet, or -1 once shut down final numPending:AtomicInt; - final isShutDown:AtomicBool; function get_scheduler():IScheduler { return s; @@ -35,7 +34,6 @@ class LuvDispatcher extends Dispatcher public function new(loop:LuvLoop, ?scheduler:IScheduler) { this.loop = loop; numPending = new AtomicInt(0); - isShutDown = new AtomicBool(false); workQueue = new AsyncDeque(loop, @:nullSafety(Off) loopWork); if (scheduler == null) { @@ -52,10 +50,17 @@ class LuvDispatcher extends Dispatcher Runs `obj` on the libuv thread pool. Throws if this dispatcher has been shut down. **/ public function dispatch(obj:IDispatchObject) { - if (isShutDown.load()) { - throw new Exception("LuvDispatcher has been shut down"); + // Increment and check in one step so that shutDown can't close the queue in between. + while (true) { + final old = numPending.load(); + if (old < 0) { + throw new Exception("LuvDispatcher has been shut down"); + } + if (numPending.compareExchange(old, old + 1) == old) { + break; + } + BackOff.backOff(); } - numPending.add(1); workQueue.add(obj); } @@ -76,11 +81,19 @@ class LuvDispatcher extends Dispatcher Must be called from the thread that runs `loop`. **/ public function shutDown() { - while (numPending.load() > 0) { - // Each finished work item wakes the loop up, so this does not miss the last one. - Luv.runLoop(loop, Once); + // Switching from 0 to -1 atomically means that no dispatch can sneak in after the check. + while (true) { + switch (numPending.compareExchange(0, -1)) { + case 0: + break; + case -1: + // Already shut down + return; + case _: + // Each finished work item wakes the loop up, so this does not miss the last one. + Luv.runLoop(loop, Once); + } } - isShutDown.store(true); workQueue.close(); switch (luvScheduler) { case Some(s): diff --git a/src/hxcoro/run/Setup.hx b/src/hxcoro/run/Setup.hx index e23dcd6e..d1a7110e 100644 --- a/src/hxcoro/run/Setup.hx +++ b/src/hxcoro/run/Setup.hx @@ -155,8 +155,10 @@ class Setup { /** Creates the default setup for the current target, as used by `CoroRun.runWith`: - `createLuv` on C++ with `hxcpp_luv_io`, a thread pool of 10 threads on JVM, C++ and - eval, and `createTrampoline` otherwise. + + - C++ with `hxcpp_luv_io`: `createLuv()` + - JVM, C++ without `hxcpp_luv_io` and eval: `createThreadPool(10)` + - Other targets: `createTrampoline()` **/ static public function createDefault() { #if (cpp && hxcpp_luv_io) diff --git a/tests/src/Main.hx b/tests/src/Main.hx index c4802679..4ee7d5ff 100644 --- a/tests/src/Main.hx +++ b/tests/src/Main.hx @@ -42,11 +42,6 @@ function main() { atest.Macros.addCases(runner, "structured"); final passed = runner.run(); - #if (cpp && hxcpp_luv_io) - // Join libuv's worker threads while the hxcpp runtime is still intact. Otherwise this - // happens in libuv's library destructor after static destructors have run. - cpp.luv.Luv.shutdown(); - #end #if sys Sys.exit(passed ? 0 : 1); #end