Project

General

Profile

Bug #15448

Kernel#raise should not write "cause" attribute destructively

Added by mame (Yusuke Endoh) about 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:90659]

Description

$ ./miniruby -e '
begin
  raise "foo"
rescue => e1
  begin
    raise "bar"
  rescue => e2
    raise e1, cause: e2
  end
end
'
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
...

This is caused by circular "cause" chain:

begin
  raise "foo"
rescue => e1
  begin
    raise "bar"
  rescue => e2
    begin
      # Virtually `e1.cause = e2` which makes the cause chain circular
      raise e1, cause: e2
    rescue
      p $!                   #=> #<RuntimeError: foo>
      p $!.cause             #=> #<RuntimeError: bar>
      p $!.cause.cause       #=> #<RuntimeError: foo>
      p $!.cause.cause.cause #=> #<RuntimeError: bar>
      p $!.cause.cause.object_id == $!.object_id
    end
  end
end

Also available in: Atom PDF