Skip to content

ConditionVariable#wait: no stale waiter after a failed wait, no lost signal at a timeout, MRI's timeout errors - #4

Closed
sholden wants to merge 1 commit into
masterfrom
fix/condvar-wait
Closed

sholden wants to merge 1 commit into
masterfrom
fix/condvar-wait

Conversation

@sholden

@sholden sholden commented Oct 29, 2026

Copy link
Copy Markdown
Owner

The wait_spec failures and the broadcast_spec NO-TALLY in Util/sweep-core.tsv are stale. That sweep dates from 2026-09-12, and 4c5d542 and 4b0848c have fixed it since. On current master all of spec/core/conditionvariable passes (11/11), including 48 repeated runs at 8 in parallel.

Comparing #wait against MRI 4.0 turned up three bugs the specs don't cover. This PR fixes them:

  • A failed #wait stayed queued. cv.wait(m) on a mutex the thread doesn't hold raised ThreadError but left the thread queued as a waiter. The next #signal went to that entry, so the real waiter slept through its whole timeout. The failed caller also kept a latched wake, so its next m.sleep(0.01) returned right away with 0 instead of nil.
  • A signal arriving as a timeout ran out was lost. #signal removed the waiter and woke it outside the lock. A waiter whose timeout expired at that moment returned nil, the signal was lost, and the AutoResetEvent stayed latched. The wake is now sent under the waiter-list lock. A waiter that finds it has been taken off the list counts as signalled: it returns the elapsed seconds and clears the latch.
  • #wait's timeout was converted like Queue#pop's. A negative timeout returned at once, where MRI raises ArgumentError: time interval must not be negative, and the wrong TypeError message was used. MRI's #wait is mutex.sleep(timeout), so it now uses Mutex#sleep's conversion. That conversion also gains MRI's RangeError for a Bignum, NaN and Infinity. RubyQueue.cs is unchanged.

Util/condvar-matrix.rb checks these cases against CRuby, and its output matches.

Testing (Linux, net10.0)

  • spec/core/conditionvariable 11/11, mutex 35/35, queue, sizedqueue, library/monitor, library/timeout: 0 failures
  • IronRuby.Tests: PASSED, 0 > FAILED
  • spec/core/thread: 395 examples, 1 failure: Thread#kill kills other fibers of that thread without running their ensure clauses. This one is a flake that predates the change. In 25 interleaved runs of kill_spec.rb each, the unmodified build failed it 6 times and this branch 2 times. It fails in every full-directory run on both builds. It uses neither Mutex nor ConditionVariable.

Not fixed

  • A Rational timeout raises TypeError in both #wait and Mutex#sleep, where MRI accepts it. That was already the case before this PR, and IO.select behaves the same way.
  • Instances print as ConditionVariable / Mutex, where MRI prints Thread::ConditionVariable. That's a constant-naming issue outside this PR.

🤖 Generated with Claude Code

…signal at a timeout, MRI's timeout errors

The wait_spec failures in Util/sweep-core.tsv were stale (fixed by 4c5d542 and 4b0848c):
spec/core/conditionvariable passes, 11 of 11. Comparing against MRI 4.0 turned up three
problems the specs do not cover:

- #wait on a mutex the thread does not hold raised ThreadError but left the thread queued
  as a waiter. The next #signal went to it: the real waiter slept through its timeout, and
  the failed caller kept a latched wake that cut its next sleep short.
- #signal woke a waiter outside the waiter-list lock, so one whose timeout ran out at the
  same moment answered nil, the signal was lost, and the wake stayed latched. The wake is
  now sent under the lock, and a waiter that finds itself taken off the list counts as
  signalled and clears the latch.
- #wait(mutex, timeout) took Queue#pop's conversion: a negative timeout returned at once
  and other types got "no implicit conversion". It now shares Mutex#sleep's conversion,
  which also learns MRI's RangeError for a Bignum, NaN and Infinity.

Util/condvar-matrix.rb diffs these against CRuby.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sholden

sholden commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

Moved upstream: Largo#42. The branch stays.

@sholden sholden closed this Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant