1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fix handling of timeout accessing scheduler outside of non-blocking context.

This commit is contained in:
Samuel Williams 2021-02-12 16:54:52 +13:00
parent af1c587546
commit 92449e0e99
Notes: git 2021-03-30 14:39:03 +09:00
4 changed files with 34 additions and 1 deletions

15
cont.c
View file

@ -1995,6 +1995,20 @@ rb_fiber_s_scheduler(VALUE klass)
return rb_fiber_scheduler_get();
}
/*
* 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
@ -3084,6 +3098,7 @@ Init_Cont(void)
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);

View file

@ -84,7 +84,7 @@ module Timeout
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

View file

@ -73,4 +73,20 @@ class TestFiberScheduler < Test::Unit::TestCase
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

View file

@ -37,6 +37,8 @@ class TestFiberTimeout < Test::Unit::TestCase
scheduler = Scheduler.new
Fiber.set_scheduler scheduler
assert_nil Fiber.current_scheduler
Timeout.timeout(1) do
message = MESSAGE
end