Skip to content

Backport fiber scheduler blocking hooks (Kernel#sleep, Mutex, ConditionVariable, Queue) to 10.0.x #10027

Open

Description

@mensfeld

Versions: JRuby 10.0.8.0 (ef393d4) and 10.0.7.0, OpenJDK 21, x86_64-linux; async 2.46.0, io-event 1.22.3.

Versions: JRuby 10.0.8.0 (ef393d4) and 10.0.7.0, OpenJDK 21, x86_64-linux; async 2.46.0, io-event 1.22.3.

Problem: on 10.0.x, Kernel#sleep, Thread::ConditionVariable#wait and Thread::Queue#pop in a non-blocking fiber never call the scheduler's kernel_sleep / block / unblock. They block the whole thread, so every
fiber on an Async reactor stalls. FiberScheduler.kernelSleep/block/unblock exist in core/src/main/java/org/jruby/FiberScheduler.java but nothing in core calls them on the 10.0 line. Fixed on master / 10.1 by #9622
(10.1.2.0), #9790 (Mutex and ConditionVariable) and #9805 (Queue) (10.1.3.0); the jruby-10.0 branch does not have them. (A contended Mutex#lock was not tested here; #9790 covers it too.)

Minimal repro:

# Logs each scheduler hook call; it waits for real (in blocking mode) so callers make progress.
class S
  def log(hook) = puts("hook #{hook}")
  def kernel_sleep(t = nil) = (log("kernel_sleep"); Fiber.blocking { Kernel.sleep(t || 0.01) })
  def block(blocker, t = nil) = (log("block #{blocker.class}"); Fiber.blocking { Kernel.sleep(t || 0.01) }; false)
  def unblock(blocker, fiber) = log("unblock #{blocker.class}")
  def io_wait(*) = log("io_wait")
  def fiber(&blk) = Fiber.new(blocking: false, &blk).tap(&:resume)
  def close = nil
end

Thread.new do
  Fiber.set_scheduler(S.new)
  Fiber.schedule do
    puts "sleep:"
    sleep 0.01
    m = Thread::Mutex.new
    cv = Thread::ConditionVariable.new
    puts "cv.wait:"
    m.synchronize { cv.wait(m, 0.01) }
    puts "queue.pop:"
    Thread::Queue.new.pop(timeout: 0.01)
    puts "done"
  end
end.join

Expected (CRuby 3.4.12, JRuby 10.1.3.0): hook kernel_sleep after sleep: and after cv.wait: (CV#wait goes through Mutex#sleep), hook block Thread::Queue after queue.pop:.

Actual (JRuby 10.0.8.0): the four labels only; no hook is called.

Impact: any Async application on 10.0.x (for example a Kafka producer that waits on a ConditionVariable for delivery) stalls the reactor for each wait.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions