Repository navigation
Block on Mutex and ConditionVariable through the fiber scheduler - #9790
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
left a comment
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()); |
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?
There was a problem hiding this comment.
| 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<>(); |
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.
There was a problem hiding this comment.
Thanks, now ConditionVariable#wait is back to no extra allocations if you're not using a scheduler.
Each thread now keeps the waiter it last used and reuses it, so ConditionVariable#wait and a contended Mutex#lock no longer allocate one every time. A Mutex only creates its queue of scheduler waiters when a fiber first needs it, and unlock skips the wakeup entirely when there is none. The awaitingThreads counter is a plain field instead of an AtomicInteger.
The cached waiter was stored as an Object because the waiter types are package-private. Make the sealed Waiter interface public so the field can be typed. It has no methods, and the records behind it stay package-private, so code outside ext.thread can't create or read one.
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.