[前][次][番号順一覧][スレッド一覧]

ruby-changes:72459

From: Jean <ko1@a...>
Date: Fri, 8 Jul 2022 02:49:37 +0900 (JST)
Subject: [ruby-changes:72459] 65ae2bb2e0 (master): Thread#value: handle threads killed by a fork

https://git.ruby-lang.org/ruby.git/commit/?id=65ae2bb2e0

From 65ae2bb2e045aa8b668d3c30515f5a6cb3eb68ad Mon Sep 17 00:00:00 2001
From: Jean Boussier <jean.boussier@g...>
Date: Thu, 7 Jul 2022 16:28:43 +0200
Subject: Thread#value: handle threads killed by a fork

[Bug #18902]

When a thread is killed because we forked, the `value` if left
to `Qundef`. Returning it woudl crash the VM.
---
 test/ruby/test_thread.rb | 14 ++++++++++++++
 thread.c                 |  4 ++++
 2 files changed, 18 insertions(+)

diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb
index cdb7309fb7..a018f3cdac 100644
--- a/test/ruby/test_thread.rb
+++ b/test/ruby/test_thread.rb
@@ -1249,6 +1249,20 @@ q.pop https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L1249
     assert_predicate(status, :success?, bug9751)
   end if Process.respond_to?(:fork)
 
+  def test_fork_value
+    bug18902 = "[Bug #18902]"
+    th = Thread.start { sleep 2 }
+    begin
+      pid = fork do
+        th.value
+      end
+      _, status = Process.wait2(pid)
+      assert_predicate(status, :success?, bug18902)
+    ensure
+      th.kill
+    end
+  end if Process.respond_to?(:fork)
+
   def test_fork_while_locked
     m = Thread::Mutex.new
     thrs = []
diff --git a/thread.c b/thread.c
index 620a56d9a0..5fd7a7f67a 100644
--- a/thread.c
+++ b/thread.c
@@ -1214,6 +1214,10 @@ thread_value(VALUE self) https://github.com/ruby/ruby/blob/trunk/thread.c#L1214
 {
     rb_thread_t *th = rb_thread_ptr(self);
     thread_join(th, Qnil, 0);
+    if (th->value == Qundef) {
+        // If the thread is dead because we forked th->value is still Qundef.
+        return Qnil;
+    }
     return th->value;
 }
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]