ruby-changes:50530
From: normal <ko1@a...>
Date: Tue, 6 Mar 2018 07:58:18 +0900 (JST)
Subject: [ruby-changes:50530] normal:r62668 (trunk): thread.c: reset waitq of keeping mutexes in child
normal 2018-03-06 07:58:13 +0900 (Tue, 06 Mar 2018) New Revision: 62668 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62668 Log: thread.c: reset waitq of keeping mutexes in child We must not maintain references to threads in the parent process in any mutexes held by the child process. * thread_sync.c (rb_mutex_cleanup_keeping_mutexes): new function * thread.c (rb_thread_atfork): cleanup keeping mutexes [ruby-core:85940] [Bug #14578] Fixes: r58604 (commit 3586c9e0876e784767a1c1adba9ebc2499fa0ec2) ("reduce rb_mutex_t size from 160 to 80 bytes on 64-bit") Modified files: trunk/test/ruby/test_thread.rb trunk/thread.c trunk/thread_sync.c Index: test/ruby/test_thread.rb =================================================================== --- test/ruby/test_thread.rb (revision 62667) +++ test/ruby/test_thread.rb (revision 62668) @@ -1205,6 +1205,17 @@ q.pop https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L1205 assert_predicate(status, :success?, bug9751) end if Process.respond_to?(:fork) + def test_fork_while_locked + m = Mutex.new + thrs = [] + 3.times do |i| + thrs << Thread.new { m.synchronize { Process.waitpid2(fork{})[1] } } + end + thrs.each do |t| + assert_predicate t.value, :success?, '[ruby-core:85940] [Bug #14578]' + end + end if Process.respond_to?(:fork) + def test_subclass_no_initialize t = Module.new do break eval("class C\u{30b9 30ec 30c3 30c9} < Thread; self; end") Index: thread.c =================================================================== --- thread.c (revision 62667) +++ thread.c (revision 62668) @@ -4236,6 +4236,7 @@ rb_thread_atfork(void) https://github.com/ruby/ruby/blob/trunk/thread.c#L4236 rb_thread_t *th = GET_THREAD(); rb_thread_atfork_internal(th, terminate_atfork_i); th->join_list = NULL; + rb_mutex_cleanup_keeping_mutexes(th); /* We don't want reproduce CVE-2003-0900. */ rb_reset_random_seed(); Index: thread_sync.c =================================================================== --- thread_sync.c (revision 62667) +++ thread_sync.c (revision 62668) @@ -415,6 +415,20 @@ rb_mutex_abandon_all(rb_mutex_t *mutexes https://github.com/ruby/ruby/blob/trunk/thread_sync.c#L415 list_head_init(&mutex->waitq); } } + +/* + * All other threads are dead in the a new child process, so waitqs + * contain references to dead threads which we need to clean up + */ +static void +rb_mutex_cleanup_keeping_mutexes(const rb_thread_t *current_thread) +{ + rb_mutex_t *mutex = current_thread->keeping_mutexes; + while (mutex) { + list_head_init(&mutex->waitq); + mutex = mutex->next_mutex; + } +} #endif static VALUE -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/