Repository navigation
Block on Mutex and ConditionVariable through the fiber scheduler - #9790
Open
sampokuokkanen wants to merge 5 commits into
Open
sampokuokkanen wants to merge 5 commits into
sampokuokkanen wants to merge 5 commits into
Conversation
A fiber waiting on a Mutex or ConditionVariable blocked its whole thread, stalling every other fiber on the scheduler. Async's Task#wait is built on these. Follow MRI's thread_sync.c and block and unblock fibers through the scheduler instead. A thread in Mutex#sleep releases the lock inside Condition#await, which can't wake a waiting fiber. So while a thread sleeps there, fibers wait on the lock with a 1ms timeout and retry.
Use a sealed Waiter interface instead of Object for the waiter queue, so the switch in wakeup is checked for exhaustiveness.
A thread that died holding a Mutex, or was interrupted right after locking it, released the lock without waking fibers waiting through the scheduler. They stayed blocked forever. Wake them from the lock's own unlock, which every release goes through, like MRI's rb_mutex_unlock_th. unlockAll now releases a copy of the held locks, since the scheduler's unblock can lock and unlock its own mutexes, and keeps going if unblock raises.
Mutex#sleep(-1) under a scheduler passed the negative timeout to kernel_sleep instead of raising ArgumentError. Check it before unlocking, like MRI, and raise ThreadError if the caller doesn't hold the mutex.
Relocking with Mutex#lock after a scheduler sleep could raise a pending interrupt, replacing the exception already on its way out and leaving the mutex unlocked. Relock without polling, like MRI's mutex_lock_uninterruptible. No spec: an interrupt raised into a fiber suspended in the sleep is taken at the fiber switch and never reaches the relock.
headius
reviewed
Oct 9, 2026
headius
left a comment
Member
There was a problem hiding this comment.
Quick review on mobile. The logic appears sound but adds allocation overhead for scheduler that is not needed otherwise. Correctness first is important but it would be good to take a second look.
| public IRubyObject wait_ruby(ThreadContext context, IRubyObject m, IRubyObject t) { | ||
| RubyThread thread = context.getThread(); | ||
| IRubyObject scheduler = FiberScheduler.current(context); | ||
| Waiter waiter = scheduler == null ? new ThreadWaiter(context.getThread()) : new Mutex.FiberWaiter(scheduler, context.getFiber()); |
Member
There was a problem hiding this comment.
Is this not something that could be allocated once and kept on the RubyThread? Allocating on every wait is pretty heavy. These waiters will only ever be associated with one RubyThread (thread or fiber) and one lock at a time, yes?
| record FiberWaiter(IRubyObject scheduler, IRubyObject fiber) implements ConditionVariable.Waiter {} | ||
|
|
||
| /** Fibers blocked in {@link #lock} through a fiber scheduler. MRI: mutex waitq */ | ||
| private final ArrayDeque<FiberWaiter> schedulerWaiters = new ArrayDeque<>(); |
Member
There was a problem hiding this comment.
Another place we only need this when a scheduler is active. I'm wondering if this could be done lazily only when needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a fiber had to wait on a Mutex or a ConditionVariable, it blocked the whole thread, so every other fiber on the same scheduler got stuck too. I ran into this with Async: Task#wait waits on a Mutex and a ConditionVariable inside Async::Promise, so waiting on one task froze all the others.
Now a fiber that has to wait in
Mutex#lock,Mutex#sleeporConditionVariable#waithands control back to the scheduler, andunlock,signalandbroadcastwake it up again.A waiting fiber also gets woken when the thread holding the lock dies, not just on a normal
unlock.There's one workaround though: a plain thread sleeping in
Mutex#sleepreleases the lock insideCondition#await, and that can't wake a fiber. For now, waiting fibers retry every 1ms while a thread is in there. I'm planning to fix this in a follow-up PR.