From 21aba1793634938e9bca8cab46f32545b5e8889a Mon Sep 17 00:00:00 2001 From: Julien Portalier Date: Tue, 11 Feb 2025 17:00:00 +0100 Subject: [PATCH] Fix: MT may fail to wake other schedulers after evloop run --- src/fiber/execution_context/multi_threaded/scheduler.cr | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fiber/execution_context/multi_threaded/scheduler.cr b/src/fiber/execution_context/multi_threaded/scheduler.cr index 866f0e9399c2..7bf532d3c571 100644 --- a/src/fiber/execution_context/multi_threaded/scheduler.cr +++ b/src/fiber/execution_context/multi_threaded/scheduler.cr @@ -142,9 +142,13 @@ module Fiber::ExecutionContext yield @global_queue.grab?(@runnables, divisor: @execution_context.size) if @execution_context.lock_evloop? { @event_loop.run(pointerof(list), blocking: false) } - if fiber = enqueue_many(pointerof(list)) + unless list.empty? + # must stop spinning before calling enqueue_many that may call + # wake_scheduler which returns immediately if a thread is + # spinning... but we're spinning, so that would always fail to + # wake sleeping schedulers despite having runnable fibers spin_stop - yield fiber + yield enqueue_many(pointerof(list)) end end