Sleep and background wakeup system I'm looking for code review of part of my replacement of timeout() project. Namely, feedback on SleeperNotifier class:
https://github.com/ledestin/frugal_timeout/blob/master/lib/frugal_timeout.rb#L125

If you would like to give feedback on the whole project, by all means do.

  # {{{1 SleeperNotifier
  # Executes callback when a request expires.
  # 1. Set callback to execute with #onExpiry=.
  # 2. Set expiry time with #expireAt.
  # 3. After the expiry time comes, execute the callback.
  #
  # It's possible to set a new expiry time before the time set previously
  # expires. In this case, processing of the old request stops and the new
  # request processing starts.
  class SleeperNotifier # :nodoc:
    include MonitorMixin

    def initialize
      super()
      @condVar, @expireAt, @onExpiry = new_cond, nil, proc {}

      @thread = Thread.new {
        loop {
          @onExpiry.call if synchronize {
            # Sleep forever until a request comes in.
            wait unless @expireAt

            timeLeft = calcTimeLeft
            disposeOfRequest
            elapsedTime = MonotonicTime.measure { wait timeLeft }

            elapsedTime &gt;= timeLeft
          }
        }
      }
      ObjectSpace.define_finalizer self, proc { @thread.kill }
    end

    def onExpiry &amp;b
      @onExpiry = b
    end

    def expireAt time
      synchronize {
        @expireAt = time
        signalThread
      }
    end

    private

    def calcTimeLeft
      synchronize {
        delay = @expireAt - MonotonicTime.now
        delay &lt; 0 ? 0 : delay
      }
    end

    def disposeOfRequest
      @expireAt = nil
    end

    def signalThread
      @condVar.signal
    end

    def wait sec=nil
      @condVar.wait sec
    end
  end
How do you cancel a sleep once it starts?  I would expect that calling .expireAt(nil) might accomplish that.  However, if @expireAt is nil, calcTimeLeft() crashes.

Alternatively, one might try to call .onExpiry(nil), but you don't handle @onExpiry being nil either.