summaryrefslogtreecommitdiff
path: root/test/fiber/scheduler.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/scheduler.rb
parent785c70e764c3222f9accac2555246d3921a7263d (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4689
Diffstat (limited to 'test/fiber/scheduler.rb')
-rw-r--r--test/fiber/scheduler.rb16
1 files changed, 14 insertions, 2 deletions
diff --git a/test/fiber/scheduler.rb b/test/fiber/scheduler.rb
index af64e4ebb6..2785561000 100644
--- a/test/fiber/scheduler.rb
+++ b/test/fiber/scheduler.rb
@@ -112,8 +112,10 @@ class Scheduler
self.run
ensure
- @urgent.each(&:close)
- @urgent = nil
+ if @urgent
+ @urgent.each(&:close)
+ @urgent = nil
+ end
@closed = true
@@ -240,3 +242,13 @@ class BrokenUnblockScheduler < Scheduler
raise "Broken unblock!"
end
end
+
+class SleepingUnblockScheduler < Scheduler
+ # This method is invoked when the thread is exiting.
+ def unblock(blocker, fiber)
+ super
+
+ # This changes the current thread state to `THREAD_RUNNING` which causes `thread_join_sleep` to hang.
+ sleep(0.1)
+ end
+end