From f150ed1532e727c409cc018a98e1362a8c71242c Mon Sep 17 00:00:00 2001 From: kosaki Date: Mon, 26 Nov 2012 08:05:49 +0000 Subject: * vm_core.h (rb_thread_struct): added 'in_trap' member for marking running trap handler. * signal.c (signal_exec): turn on in_trap when running trap. * thread.c (Init_Thread, thread_create_core): initialize in_trap when creating new threads. * thread.c (thread_join_m): raise ThreadError when running trap handler.Bug [#6416][ruby-core:44956] * test/ruby/test_thread.rb (test_thread_join_in_trap): new test for the above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37852 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 12 ++++++++++++ signal.c | 18 +++++++++++++++++- test/ruby/test_thread.rb | 15 +++++++++++++++ thread.c | 9 +++++++++ vm_core.h | 3 +++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 6bb69adea1..35a8f72918 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Mon Nov 26 17:00:12 2012 KOSAKI Motohiro + + * vm_core.h (rb_thread_struct): added 'in_trap' member for marking + running trap handler. + * signal.c (signal_exec): turn on in_trap when running trap. + * thread.c (Init_Thread, thread_create_core): initialize in_trap + when creating new threads. + * thread.c (thread_join_m): raise ThreadError when running trap + handler.Bug [#6416][ruby-core:44956] + * test/ruby/test_thread.rb (test_thread_join_in_trap): new test + for the above. + Mon Nov 26 16:36:13 2012 NARUSE, Yui * io.c (argf_each_codepoint): add missing ARGF#codepoints [Bug #7438] diff --git a/signal.c b/signal.c index d97d297c1c..9f1717e012 100644 --- a/signal.c +++ b/signal.c @@ -17,6 +17,7 @@ #include #include #include "ruby_atomic.h" +#include "eval_intern.h" #if defined(__native_client__) && defined(NACL_NEWLIB) # include "nacl/signal.h" @@ -623,7 +624,22 @@ static void signal_exec(VALUE cmd, int safe, int sig) { VALUE signum = INT2NUM(sig); - rb_eval_cmd(cmd, rb_ary_new3(1, signum), safe); + rb_thread_t *cur_th = GET_THREAD(); + int old_in_trap = cur_th->in_trap; + int state; + + cur_th->in_trap = 1; + TH_PUSH_TAG(cur_th); + if ((state = EXEC_TAG()) == 0) { + rb_eval_cmd(cmd, rb_ary_new3(1, signum), safe); + } + TH_POP_TAG(); + cur_th->in_trap = old_in_trap; + + if (state) { + /* XXX: should be replaced with rb_threadptr_async_errinfo_enque() */ + JUMP_TAG(state); + } } void diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb index 145e021790..518ad40333 100644 --- a/test/ruby/test_thread.rb +++ b/test/ruby/test_thread.rb @@ -863,4 +863,19 @@ class TestThreadGroup < Test::Unit::TestCase end assert_in_delta(t1 - t0, 1, 1, bug5757) end + + def test_thread_join_in_trap + assert_raise(ThreadError) { + t = Thread.new{ sleep 0.2; Process.kill(:INT, $$) } + + Signal.trap :INT do + t.join + end + + t.join + } + end + + + end diff --git a/thread.c b/thread.c index 64eaadc9b5..3af7a0fee4 100644 --- a/thread.c +++ b/thread.c @@ -567,6 +567,8 @@ thread_create_core(VALUE thval, VALUE args, VALUE (*fn)(ANYARGS)) th->async_errinfo_mask_stack = rb_ary_dup(current_th->async_errinfo_mask_stack); RBASIC(th->async_errinfo_mask_stack)->klass = 0; + th->in_trap = 0; + native_mutex_initialize(&th->interrupt_lock); /* kick thread */ @@ -790,9 +792,14 @@ static VALUE thread_join_m(int argc, VALUE *argv, VALUE self) { rb_thread_t *target_th; + rb_thread_t *cur_th = GET_THREAD(); double delay = DELAY_INFTY; VALUE limit; + if (cur_th->in_trap) { + rb_raise(rb_eThreadError, "can't be called from trap context"); + } + GetThreadPtr(self, target_th); rb_scan_args(argc, argv, "01", &limit); @@ -4773,6 +4780,8 @@ Init_Thread(void) th->async_errinfo_queue = rb_ary_tmp_new(0); th->async_errinfo_queue_checked = 0; th->async_errinfo_mask_stack = rb_ary_tmp_new(0); + + th->in_trap = 0; } } diff --git a/vm_core.h b/vm_core.h index fffaa5e986..333e845607 100644 --- a/vm_core.h +++ b/vm_core.h @@ -584,6 +584,9 @@ typedef struct rb_thread_struct { void *altstack; #endif unsigned long running_time_us; + + /* 1 if running trap handler */ + int in_trap; } rb_thread_t; /* iseq.c */ -- cgit v1.2.3