From: "kjtsanaktsidis (KJ Tsanaktsidis) via ruby-core" Date: 2023-06-11T13:32:56+00:00 Subject: [ruby-core:113869] [Ruby master Bug#19717] `ConditionVariable#signal` is not fair when the wakeup is consistently spurious. Issue #19717 has been updated by kjtsanaktsidis (KJ Tsanaktsidis). > have ConditionVariable#wait take an optional kwarg that lets the caller report whether their last wakeup was spurious or no A better API might be to have the `ConditionVariable#wait` accept a block, and have Ruby do the looping & checking of the condition. Something like ``` @condvar.wait(mutex) do @resource.available? end ``` That's probably a nicer Ruby API anyway, and means it could transparently implement something like "append to the waitq on the first wait, and prepend thereafter". ---------------------------------------- Bug #19717: `ConditionVariable#signal` is not fair when the wakeup is consistently spurious. https://bugs.ruby-lang.org/issues/19717#change-103517 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- For background, see this issue . It looks like `ConditionVariable#signal` is not fair, if the calling thread immediately reacquires the resource. I've given a detailed reproduction here as it's non-trivial: . Because the spurious wakeup occurs, the thread is pushed to the back of the waitq, which means any other waiting thread will acquire the resource, and that thread will perpetually be at the back of the queue. I believe the solution is to change `ConditionVarialbe#signal` should only remove the thread from the waitq if it's possible to acquire the lock. Otherwise it should be left in place, so that the order is retained, this should result in fair scheduling. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/