ruby-changes:65707
From: Samuel <ko1@a...>
Date: Tue, 30 Mar 2021 14:39:02 +0900 (JST)
Subject: [ruby-changes:65707] 92449e0e99 (master): Fix handling of timeout accessing scheduler outside of non-blocking context.
https://git.ruby-lang.org/ruby.git/commit/?id=92449e0e99 From 92449e0e99ae0e44f0deec5e59a7146824872952 Mon Sep 17 00:00:00 2001 From: Samuel Williams <samuel.williams@o...> Date: Fri, 12 Feb 2021 16:54:52 +1300 Subject: Fix handling of timeout accessing scheduler outside of non-blocking context. --- cont.c | 15 +++++++++++++++ lib/timeout.rb | 2 +- test/fiber/test_scheduler.rb | 16 ++++++++++++++++ test/fiber/test_timeout.rb | 2 ++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cont.c b/cont.c index 0d95a09..bdd3085 100644 --- a/cont.c +++ b/cont.c @@ -1997,6 +1997,20 @@ rb_fiber_s_scheduler(VALUE klass) https://github.com/ruby/ruby/blob/trunk/cont.c#L1997 /* * call-seq: + * Fiber.current_scheduler -> obj or nil + * + * Returns the Fiber scheduler, that was last set for the current thread with Fiber.set_scheduler + * iff the current fiber is non-blocking. + * + */ +static VALUE +rb_fiber_current_scheduler(VALUE klass) +{ + return rb_fiber_scheduler_current(); +} + +/* + * call-seq: * Fiber.set_scheduler(scheduler) -> scheduler * * Sets the Fiber scheduler for the current thread. If the scheduler is set, non-blocking @@ -3084,6 +3098,7 @@ Init_Cont(void) https://github.com/ruby/ruby/blob/trunk/cont.c#L3098 rb_define_singleton_method(rb_cFiber, "blocking?", rb_fiber_s_blocking_p, 0); rb_define_singleton_method(rb_cFiber, "scheduler", rb_fiber_s_scheduler, 0); rb_define_singleton_method(rb_cFiber, "set_scheduler", rb_fiber_set_scheduler, 1); + rb_define_singleton_method(rb_cFiber, "current_scheduler", rb_fiber_current_scheduler, 0); rb_define_singleton_method(rb_cFiber, "schedule", rb_fiber_s_schedule, -1); diff --git a/lib/timeout.rb b/lib/timeout.rb index dc8eb24..11db4be 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -84,7 +84,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L84 message ||= "execution expired".freeze - if (scheduler = Fiber.scheduler)&.respond_to?(:timeout_after) + if (scheduler = Fiber.current_scheduler)&.respond_to?(:timeout_after) return scheduler.timeout_after(sec, klass || Error, message, &block) end diff --git a/test/fiber/test_scheduler.rb b/test/fiber/test_scheduler.rb index 72bde9f..eeb0d67 100644 --- a/test/fiber/test_scheduler.rb +++ b/test/fiber/test_scheduler.rb @@ -73,4 +73,20 @@ class TestFiberScheduler < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/fiber/test_scheduler.rb#L73 thread.join end + + def test_current_scheduler + thread = Thread.new do + scheduler = Scheduler.new + Fiber.set_scheduler scheduler + + assert Fiber.scheduler + refute Fiber.current_scheduler + + Fiber.schedule do + assert Fiber.current_scheduler + end + end + + thread.join + end end diff --git a/test/fiber/test_timeout.rb b/test/fiber/test_timeout.rb index c17092b..5493606 100644 --- a/test/fiber/test_timeout.rb +++ b/test/fiber/test_timeout.rb @@ -37,6 +37,8 @@ class TestFiberTimeout < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/fiber/test_timeout.rb#L37 scheduler = Scheduler.new Fiber.set_scheduler scheduler + assert_nil Fiber.current_scheduler + Timeout.timeout(1) do message = MESSAGE end -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/