Skip to content

Commit

Permalink
Fix: dequeue one fiber every once in a while (MT)
Browse files Browse the repository at this point in the history
We previously tried to grab fibers from the global queue which calls
`Runnables#push` that may overflow the local queue and thus try to push
to the global queue, leading to a deadlock.

Replaces the every once in a while dequeue from the global queue with a
single dequeue, which returns one fiber that can be resumed immediately.
  • Loading branch information
ysbaddaden committed Mar 11, 2024
1 parent 461490e commit 6046505
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/multi_threaded/scheduler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ abstract class ExecutionContext
private def dequeue? : Fiber?
# every once in a while: dequeue from global queue to avoid two fibers
# constantly respawing each other to completely occupy the local queue
#
# FIXME: dequeue one or grab a batch of fibers (?)
if (@tick &+= 1) % 61 == 0
if fiber = global_dequeue?
if fiber = @execution_context.global_queue.pop?
return fiber
end
end
Expand All @@ -108,7 +106,7 @@ abstract class ExecutionContext
return fiber
end

# dequeue from global queue
# dequeue from global queue (tries to refill local queue)
if fiber = global_dequeue?
return fiber
end
Expand Down

0 comments on commit 6046505

Please sign in to comment.