[前][次][番号順一覧][スレッド一覧]

ruby-changes:44052

From: nobu <ko1@a...>
Date: Fri, 9 Sep 2016 17:59:53 +0900 (JST)
Subject: [ruby-changes:44052] nobu:r56125 (trunk): thread.c: set cause by Thread#raise

nobu	2016-09-09 17:59:48 +0900 (Fri, 09 Sep 2016)

  New Revision: 56125

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56125

  Log:
    thread.c: set cause by Thread#raise
    
    * thread.c (rb_threadptr_raise): set cause from the called thread,
      but not from the thread to be interrupted.
      [ruby-core:77222] [Bug #12741]

  Modified files:
    trunk/ChangeLog
    trunk/eval.c
    trunk/test/ruby/test_exception.rb
    trunk/thread.c
Index: eval.c
===================================================================
--- eval.c	(revision 56124)
+++ eval.c	(revision 56125)
@@ -567,6 +567,17 @@ setup_exception(rb_thread_t *th, int tag https://github.com/ruby/ruby/blob/trunk/eval.c#L567
     }
 }
 
+void
+rb_threadptr_setup_exception(rb_thread_t *th, VALUE mesg, VALUE cause)
+{
+    if (cause == Qundef) {
+	cause = get_thread_errinfo(th);
+    }
+    if (cause != mesg) {
+	rb_ivar_set(mesg, id_cause, cause);
+    }
+}
+
 static void
 rb_longjmp(int tag, volatile VALUE mesg, VALUE cause)
 {
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56124)
+++ ChangeLog	(revision 56125)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Sep  9 17:59:46 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* thread.c (rb_threadptr_raise): set cause from the called thread,
+	  but not from the thread to be interrupted.
+	  [ruby-core:77222] [Bug #12741]
+
 Fri Sep  9 13:50:05 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* doc/extension.rdoc, doc/extension.ja.rdoc: fix file name.
Index: test/ruby/test_exception.rb
===================================================================
--- test/ruby/test_exception.rb	(revision 56124)
+++ test/ruby/test_exception.rb	(revision 56125)
@@ -698,6 +698,52 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L698
     assert_nil(e.cause.cause)
   end
 
+  def test_cause_thread_no_cause
+    bug12741 = '[ruby-core:77222] [Bug #12741]'
+
+    x = Thread.current
+    a = false
+    y = Thread.start do
+      Thread.pass until a
+      x.raise "stop"
+    end
+
+    begin
+      raise bug12741
+    rescue
+      e = assert_raise_with_message(RuntimeError, "stop") do
+        a = true
+        sleep 1
+      end
+    end
+    assert_nil(e.cause)
+  end
+
+  def test_cause_thread_with_cause
+    bug12741 = '[ruby-core:77222] [Bug #12741]'
+
+    x = Thread.current
+    a = false
+    y = Thread.start do
+      Thread.pass until a
+      begin
+        raise "caller's cause"
+      rescue
+        x.raise "stop"
+      end
+    end
+
+    begin
+      raise bug12741
+    rescue
+      e = assert_raise_with_message(RuntimeError, "stop") do
+        a = true
+        sleep 1
+      end
+    end
+    assert_equal("caller's cause", e.cause.message)
+  end
+
   def test_unknown_option
     bug = '[ruby-core:63203] [Feature #8257] should pass unknown options'
 
Index: thread.c
===================================================================
--- thread.c	(revision 56124)
+++ thread.c	(revision 56125)
@@ -2084,6 +2084,8 @@ rb_threadptr_ready(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread.c#L2084
     rb_threadptr_interrupt(th);
 }
 
+void rb_threadptr_setup_exception(rb_thread_t *th, VALUE mesg, VALUE cause);
+
 static VALUE
 rb_threadptr_raise(rb_thread_t *th, int argc, VALUE *argv)
 {
@@ -2099,6 +2101,7 @@ rb_threadptr_raise(rb_thread_t *th, int https://github.com/ruby/ruby/blob/trunk/thread.c#L2101
     else {
 	exc = rb_make_exception(argc, argv);
     }
+    rb_threadptr_setup_exception(GET_THREAD(), exc, Qundef);
     rb_threadptr_pending_interrupt_enque(th, exc);
     rb_threadptr_interrupt(th);
     return Qnil;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]