summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--process.c5
-rw-r--r--spec/ruby/core/kernel/sleep_spec.rb27
2 files changed, 27 insertions, 5 deletions
diff --git a/process.c b/process.c
index 2c29af787b..705483bdc4 100644
--- a/process.c
+++ b/process.c
@@ -4918,6 +4918,9 @@ rb_f_spawn(int argc, VALUE *argv, VALUE _)
* thread calls Thread#run. Called without an argument, sleep()
* will sleep forever.
*
+ * If the +duration+ is not supplied, or is +nil+, the thread sleeps forever.
+ * Threads in this state may still be interrupted by other threads.
+ *
* Time.new #=> 2008-03-08 19:56:19 +0900
* sleep 1.2 #=> 1
* Time.new #=> 2008-03-08 19:56:20 +0900
@@ -4935,7 +4938,7 @@ rb_f_sleep(int argc, VALUE *argv, VALUE _)
rb_fiber_scheduler_kernel_sleepv(scheduler, argc, argv);
}
else {
- if (argc == 0) {
+ if (argc == 0 || (argc == 1 && NIL_P(argv[0]))) {
rb_thread_sleep_forever();
}
else {
diff --git a/spec/ruby/core/kernel/sleep_spec.rb b/spec/ruby/core/kernel/sleep_spec.rb
index 32da6344c1..44b417a92e 100644
--- a/spec/ruby/core/kernel/sleep_spec.rb
+++ b/spec/ruby/core/kernel/sleep_spec.rb
@@ -33,10 +33,6 @@ describe "Kernel#sleep" do
-> { sleep(-1) }.should raise_error(ArgumentError)
end
- it "raises a TypeError when passed nil" do
- -> { sleep(nil) }.should raise_error(TypeError)
- end
-
it "raises a TypeError when passed a String" do
-> { sleep('2') }.should raise_error(TypeError)
end
@@ -55,6 +51,29 @@ describe "Kernel#sleep" do
t.wakeup
t.value.should == 5
end
+
+ ruby_version_is ""..."3.3" do
+ it "raises a TypeError when passed nil" do
+ -> { sleep(nil) }.should raise_error(TypeError)
+ end
+ end
+
+ ruby_version_is "3.3" do
+ it "accepts a nil duration" do
+ running = false
+ t = Thread.new do
+ running = true
+ sleep(nil)
+ 5
+ end
+
+ Thread.pass until running
+ Thread.pass while t.status and t.status != "sleep"
+
+ t.wakeup
+ t.value.should == 5
+ end
+ end
end
describe "Kernel.sleep" do