summaryrefslogtreecommitdiff
path: root/test/fiber/test_thread.rb
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2021-07-28 19:55:55 +1200
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2021-08-03 22:23:48 +1200
commit2d4f29e77e883c29e35417799f8001b8046cde03 (patch)
treec2201611275519a9f36b7d3d3ce79583bc7ba14f /test/fiber/test_thread.rb
parent785c70e764c3222f9accac2555246d3921a7263d (diff)
downloadruby-2d4f29e77e883c29e35417799f8001b8046cde03.tar.gz
Fix potential hang when joining threads.
If the thread termination invokes user code after `th->status` becomes `THREAD_KILLED`, and the user unblock function causes that `th->status` to become something else (e.g. `THREAD_RUNNING`), threads waiting in `thread_join_sleep` will hang forever. We move the unblock function call to before the thread status is updated, and allow threads to join as soon as `th->value` becomes defined. This reverts commit 6505c77501f1924571b2fe620c5c7b31ede0cd22.
Diffstat (limited to 'test/fiber/test_thread.rb')
-rw-r--r--test/fiber/test_thread.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/test/fiber/test_thread.rb b/test/fiber/test_thread.rb
index 843604b5f1..5c25c43de2 100644
--- a/test/fiber/test_thread.rb
+++ b/test/fiber/test_thread.rb
@@ -20,6 +20,31 @@ class TestFiberThread < Test::Unit::TestCase
assert_equal :done, thread.value
end
+ def test_thread_join_implicit
+ sleeping = false
+ finished = false
+
+ thread = Thread.new do
+ scheduler = Scheduler.new
+ Fiber.set_scheduler scheduler
+
+ Fiber.schedule do
+ sleeping = true
+ sleep(0.1)
+ finished = true
+ end
+
+ :done
+ end
+
+ Thread.pass until sleeping
+
+ thread.join
+
+ assert_equal :done, thread.value
+ assert finished, "Scheduler thread's task should be finished!"
+ end
+
def test_thread_join_blocking
thread = Thread.new do
scheduler = Scheduler.new
@@ -66,4 +91,18 @@ class TestFiberThread < Test::Unit::TestCase
thread.join
end
end
+
+ def test_thread_join_hang
+ thread = Thread.new do
+ scheduler = SleepingUnblockScheduler.new
+
+ Fiber.set_scheduler scheduler
+
+ Fiber.schedule do
+ Thread.new{sleep(0.01)}.value
+ end
+ end
+
+ thread.join
+ end
end