Skip to content

Mutex#sleep and ConditionVariable#wait take a Rational, a BigDecimal or a long as their timeout - #7

Closed
sholden wants to merge 2 commits into
fix/condvar-waitfrom
fix/sleep-interval-divmod
Closed

sholden wants to merge 2 commits into
fix/condvar-waitfrom
fix/sleep-interval-divmod

Conversation

@sholden

@sholden sholden commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Stacked on #4. It builds on the shared RubyMutex.ToSleepSeconds conversion added there.

MRI converts a timeout that isn't an Integer or a Float through divmod(1). The whole seconds go through to_int, and the fraction is multiplied by 1_000_000_000 to give nanoseconds. Anything without a #divmod that returns an Array raises "can't convert X into time interval". IronRuby accepted only an int or a double, so these raised TypeError:

  • m.sleep(Rational(1, 100)) and cv.wait(m, BigDecimal("0.01")), which work in MRI.
  • m.sleep(2**40), which MRI sleeps on. IronRuby stores an Integer past 32 bits as a CLR long, and the conversion didn't handle that type.

A Bignum now raises MRI's RangeError only when it doesn't fit a long. Initializers.Generated.cs was regenerated with ClassInitGenerator, and the regenerated file differs only in the two changed signatures (8 lines).

Testing (Linux, net10.0)

Still different from MRI (not in this PR)

  • Kernel#sleep has its own conversion. A negative Rational gives "time interval must be positive" (MRI: "must not be negative"). Objects that aren't intervals get NoMethodError or "no implicit conversion" instead of "can't convert X into time interval".
  • Error message wording. [nil, 0] from #divmod reports IronRuby's generic "no implicit conversion of nil into Integer" (MRI: "from nil to integer"). NoMethodError messages use IronRuby's backtick quoting.
  • A negative fraction (divmod → [0, -0.05]) raises ArgumentError here. MRI blocks forever.

🤖 Generated with Claude Code

sholden and others added 2 commits October 29, 2026 03:08
…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]>
…or a long as their timeout

MRI turns a timeout that is not an Integer or a Float into an interval through
divmod(1): the whole seconds through #to_int, the fraction times 1_000_000_000 as
nanoseconds; an object without a #divmod that answers an Array is "can't convert X
into time interval". IronRuby only took an int or a double, so a Rational or a
BigDecimal was a TypeError - and so was any Integer past 32 bits, which IronRuby holds
as a long (m.sleep(2**40) raised instead of sleeping). A Bignum is MRI's RangeError
only when it does not fit a long.

Initializers.Generated.cs regenerated with ClassInitGenerator: the two signatures only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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