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

merge revision(s) 65ae2bb2e0: [Backport #18902]

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(+)
This commit is contained in:
nagachika 2022-09-25 12:33:30 +09:00
parent 2fb900e6a0
commit 720de2008c
3 changed files with 20 additions and 2 deletions

View file

@ -1244,6 +1244,20 @@ q.pop
assert_predicate(status, :success?, bug9751) assert_predicate(status, :success?, bug9751)
end if Process.respond_to?(:fork) 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 def test_fork_while_locked
m = Thread::Mutex.new m = Thread::Mutex.new
thrs = [] thrs = []

View file

@ -1364,6 +1364,10 @@ thread_value(VALUE self)
{ {
rb_thread_t *th = rb_thread_ptr(self); rb_thread_t *th = rb_thread_ptr(self);
thread_join(th, Qnil, 0); 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; return th->value;
} }

View file

@ -11,11 +11,11 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 3 #define RUBY_VERSION_TEENY 3
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 56 #define RUBY_PATCHLEVEL 57
#define RUBY_RELEASE_YEAR 2022 #define RUBY_RELEASE_YEAR 2022
#define RUBY_RELEASE_MONTH 9 #define RUBY_RELEASE_MONTH 9
#define RUBY_RELEASE_DAY 20 #define RUBY_RELEASE_DAY 25
#include "ruby/version.h" #include "ruby/version.h"