Skip to content

Defer Kernel#sleep to the Fiber scheduler - #9622

Merged
headius merged 1 commit into
jruby:masterfrom
sampokuokkanen:fiber-scheduler-kernel-sleep
Sep 3, 2026
Merged

headius merged 1 commit into
jruby:masterfrom
sampokuokkanen:fiber-scheduler-kernel-sleep

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

Kernel#sleep never consulted Fiber.scheduler, so a non-blocking Fiber that slept parked its thread instead of yielding. Follow MRI's rb_f_sleep: look up the current scheduler first and hand it the arguments untouched, nil included, before any interval conversion. FiberScheduler.kernelSleep was already written, this just wires it up.

Closing the scheduler at exit is also fixed. MRI's rb_ec_teardown sets the thread's scheduler to nil before at-exit handlers, which runs out the fibers still waiting on it; JRuby only did that for spawned threads, through RubyThread#dispose. Harmless until now, since nothing parked a fiber on the main thread. But with sleep deferring, a fiber sleeping there would never be resumed, skipping the rest of its body and its ensure blocks.

Passes the tests in ruby/spec#1391, while master fails the new "closes the scheduler at exit" spec.
Follow-up needed: wire Thread::Queue#pop and Thread::Mutex#lock to FiberScheduler.block/unblock and delete the test exclusions added in this PR.

Kernel#sleep never consulted Fiber.scheduler, so a non-blocking Fiber that slept parked its thread instead of yielding. Follow MRI's rb_f_sleep: look up the current scheduler first and hand it the arguments untouched, nil included, before any interval conversion. FiberScheduler.kernelSleep was already written, this just wires it up.

Closing the scheduler at exit is also fixed. MRI's rb_ec_teardown sets the thread's scheduler to nil before at-exit handlers, which runs out the fibers still waiting on it; JRuby only did that for spawned threads, through RubyThread#dispose. Harmless until now, since nothing parked a fiber on the main thread. But with sleep deferring, a fiber sleeping there would never be resumed, skipping the rest of its body and its ensure blocks.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, good catch. Since I originally implemented the thread scheduler stuff, more blocking operations have steadily been added. Good to get sleep checked off the list.

@headius
headius merged commit d1dbacf into jruby:master Sep 3, 2026
132 of 134 checks passed
@headius headius added this to the JRuby 10.1.2.0 milestone Sep 3, 2026
@sampokuokkanen
sampokuokkanen deleted the fiber-scheduler-kernel-sleep branch November 15, 2026 00:39
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.

2 participants