diff --git a/eval.c b/eval.c index 17a3f2743ff336..15a1c25076b398 100644 --- a/eval.c +++ b/eval.c @@ -767,6 +767,12 @@ rb_exception_setup(int argc, VALUE *argv) if (resolved_cause == exception) { resolved_cause = Qnil; } + + // A non-local exit in flight keeps its throw data in errinfo, and + // throw data is not an exception: + if (THROW_DATA_P(resolved_cause)) { + resolved_cause = Qnil; + } } else if (NIL_P(cause)) { // Explicit nil cause - prevent chaining: diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index 0562897afe47b7..0a8a177ea82a4d 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -949,6 +949,37 @@ def test_cause_thread_with_cause assert_equal("caller's cause", e.cause.message) end + # Thread#raise and Fiber#raise take their implicit cause from errinfo, which + # holds throw data while a non-local exit is in flight. The exception queued + # here is delivered when Thread.handle_interrupt returns, and leaving the + # block with `break` makes that happen while the break is still unwinding. + # The VM calls #exception on the queued object at that point. + def assert_cause_not_throw_data(raiser) + assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}") + begin; + e = nil + begin + Thread.handle_interrupt(Object => :never) do + queued = StandardError.new("queued") + Thread.current.raise(queued) + queued.define_singleton_method(:exception) {|*| #{raiser}.raise("in flight")} + break + end + rescue => e + end + assert_equal("in flight", e.message) + assert_nil(e.cause) + end; + end + + def test_cause_thread_raise_not_throw_data + assert_cause_not_throw_data("Thread.current") + end + + def test_cause_fiber_raise_not_throw_data + assert_cause_not_throw_data("Fiber.current") + end + def test_unknown_option bug = '[ruby-core:63203] [Feature #8257] should pass unknown options'