Skip to content

Commit

Permalink
* vm_core.h (rb_thread_struct): added 'in_trap' member for marking
Browse files Browse the repository at this point in the history
  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
  • Loading branch information
kosaki committed Nov 26, 2012
1 parent 3e0becb commit f150ed1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Mon Nov 26 17:00:12 2012 KOSAKI Motohiro <kosaki.motohiro@gmail.com>

* 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 <naruse@ruby-lang.org>

* io.c (argf_each_codepoint): add missing ARGF#codepoints [Bug #7438]
Expand Down
18 changes: 17 additions & 1 deletion signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdio.h>
#include <errno.h>
#include "ruby_atomic.h"
#include "eval_intern.h"

#if defined(__native_client__) && defined(NACL_NEWLIB)
# include "nacl/signal.h"
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/ruby/test_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -863,4 +863,19 @@ def test_thread_timer_and_interrupt
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
9 changes: 9 additions & 0 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

Expand Down
3 changes: 3 additions & 0 deletions vm_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit f150ed1

Please sign in to comment.