From afa512d9e18d39b2e727a0ee1a792f21505779bb Mon Sep 17 00:00:00 2001 From: normal Date: Fri, 12 Sep 2014 19:42:01 +0000 Subject: [PATCH] Process.detach: avoid singleton class creation * process.c (Init_process): subclass Thread as Process::Waiter (rb_detach_process): use Process::Waiter instead of singleton class * test/ruby/test_process.rb (test_process_detach): new test * inits.c (rb_call_inits): call Init_Thread before Init_process to ensure Process::Waiter may be a subclass of Thread Thanks to headius for reporting [Bug #10231] Thanks to nobu for review of my initial patch. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47561 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ inits.c | 2 +- process.c | 9 ++++++++- test/ruby/test_process.rb | 8 ++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 475fec5162..91b4819cb2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Sat Sep 13 04:40:04 2014 Eric Wong + + * process.c (Init_process): subclass Thread as Process::Waiter + (rb_detach_process): use Process::Waiter instead of singleton class + Thanks to headius and nobu. [Bug #10231] + + * test/ruby/test_process.rb (test_process_detach): new test + + * inits.c (rb_call_inits): call Init_Thread before Init_process to + ensure Process::Waiter may be a subclass of Thread + Fri Sep 12 18:14:28 2014 Eric Wong * vm.c (env_alloc): inline to avoid extra zeroing diff --git a/inits.c b/inits.c index fe0aade090..da8cfb1ed9 100644 --- a/inits.c +++ b/inits.c @@ -46,7 +46,6 @@ rb_call_inits(void) CALL(Time); CALL(Random); CALL(signal); - CALL(process); CALL(load); CALL(Proc); CALL(Binding); @@ -56,6 +55,7 @@ rb_call_inits(void) CALL(VM); CALL(ISeq); CALL(Thread); + CALL(process); CALL(Cont); CALL(Rational); CALL(Complex); diff --git a/process.c b/process.c index 8bb52f7b77..590a21fa45 100644 --- a/process.c +++ b/process.c @@ -1007,6 +1007,8 @@ proc_waitall(void) return result; } +static VALUE rb_cWaiter; + static inline ID id_pid(void) { @@ -1038,7 +1040,7 @@ rb_detach_process(rb_pid_t pid) { VALUE watcher = rb_thread_create(detach_process_watcher, (void*)(VALUE)pid); rb_thread_local_aset(watcher, id_pid(), PIDT2NUM(pid)); - rb_define_singleton_method(watcher, "pid", detach_process_pid, 0); + RBASIC_SET_CLASS(watcher, rb_cWaiter); return watcher; } @@ -7516,6 +7518,11 @@ Init_process(void) rb_define_module_function(rb_mProcess, "waitall", proc_waitall, 0); rb_define_module_function(rb_mProcess, "detach", proc_detach, 1); + rb_cWaiter = rb_define_class_under(rb_mProcess, "Waiter", rb_cThread); + rb_undef_alloc_func(rb_cWaiter); + rb_undef_method(CLASS_OF(rb_cWaiter), "new"); + rb_define_method(rb_cWaiter, "pid", detach_process_pid, 0); + rb_cProcessStatus = rb_define_class_under(rb_mProcess, "Status", rb_cObject); rb_undef_method(CLASS_OF(rb_cProcessStatus), "new"); diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb index 608d663027..0530c8d088 100644 --- a/test/ruby/test_process.rb +++ b/test/ruby/test_process.rb @@ -1965,4 +1965,12 @@ EOS runner.close end end if defined?(fork) + + def test_process_detach + pid = fork {} + th = Process.detach(pid) + assert_equal pid, th.pid + status = th.value + assert status.success?, status.inspect + end if defined?(fork) end