ruby-changes:47856
From: normal <ko1@a...>
Date: Thu, 21 Sep 2017 03:47:20 +0900 (JST)
Subject: [ruby-changes:47856] normal:r59975 (trunk): process: block/unblock signals around fork
normal 2017-09-21 03:47:14 +0900 (Thu, 21 Sep 2017) New Revision: 59975 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59975 Log: process: block/unblock signals around fork As with forking for execve(2) in `spawn', we must block signals to ensure they are handled correctly in a freshly `fork'-ed child. * process.c (retry_fork_ruby): block/unblock signals around fork (rb_fork_ruby): re-enable signals in forked child * test/ruby/test_process.rb (test_forked_child_signal): new test [ruby-core:82883] [Bug #13916] Thanks to Russell Davis for the bug report and test case. Modified files: trunk/process.c trunk/test/ruby/test_process.rb Index: test/ruby/test_process.rb =================================================================== --- test/ruby/test_process.rb (revision 59974) +++ test/ruby/test_process.rb (revision 59975) @@ -2329,4 +2329,14 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/ruby/test_process.rb#L2329 end end end + + def test_forked_child_handles_signal + skip "fork not supported" unless Process.respond_to?(:fork) + assert_normal_exit(<<-"end;", '[ruby-core:82883] [Bug #13916]') + require 'timeout' + pid = fork { sleep } + Process.kill(:TERM, pid) + assert_equal pid, Timeout.timeout(30) { Process.wait(pid) } + end; + end end Index: process.c =================================================================== --- process.c (revision 59974) +++ process.c (revision 59975) @@ -3633,7 +3633,7 @@ rb_fork_async_signal_safe(int *status, i https://github.com/ruby/ruby/blob/trunk/process.c#L3633 } static rb_pid_t -retry_fork_ruby(int *status) +retry_fork_ruby(int *status, struct child_handler_disabler_state *old) { rb_pid_t pid; int try_gc = 1; @@ -3641,10 +3641,12 @@ retry_fork_ruby(int *status) https://github.com/ruby/ruby/blob/trunk/process.c#L3641 while (1) { prefork(); before_fork_ruby(); + disable_child_handler_before_fork(old); pid = fork(); if (pid == 0) /* fork succeed, child process */ return pid; preserving_errno(after_fork_ruby()); + preserving_errno(disable_child_handler_fork_parent(old)); if (0 < pid) /* fork succeed, parent process */ return pid; /* fork failed */ @@ -3657,14 +3659,16 @@ rb_pid_t https://github.com/ruby/ruby/blob/trunk/process.c#L3659 rb_fork_ruby(int *status) { rb_pid_t pid; + struct child_handler_disabler_state old; if (status) *status = 0; - pid = retry_fork_ruby(status); + pid = retry_fork_ruby(status, &old); if (pid < 0) return pid; if (!pid) { after_fork_ruby(); + disable_child_handler_fork_parent(&old); /* yes, bad name */ } return pid; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/