Project

General

Profile

Bug #12068

raise overwrites exception cause even if exception is not new

Added by headius (Charles Nutter) over 3 years ago. Updated about 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:73792]

Description

It appears that raise will overwrite the original cause of an exception even when re-raising the same one. I believe this is a bug, since loses the original cause and associates the re-raised exception with a completely unrelated cause.

Example:

begin
  begin
    raise 'a'
  rescue => a
    begin
      raise 'b'
    rescue => b
      p [b, b.cause]
      begin
        raise 'c'
      rescue
        raise b
      end
    end
  end
rescue
  p [$!, $!.cause]
end

CRuby outputs:

[] ~/projects/jruby $ ruby23 cause.rb
[#<RuntimeError: b>, #<RuntimeError: a>]
[#<RuntimeError: b>, #<RuntimeError: c>]

Here, the original cause of the "b" exception (the "a" exception) is lost, replaced during the re-raise with the "c" exception.

I believe JRuby 9.0.5.0's behavior is correct here:

[] ~/projects/jruby $ rvm jruby-9.0.5.0 do ruby cause.rb
[#<RuntimeError: b>, #<RuntimeError: a>]
[#<RuntimeError: b>, #<RuntimeError: a>]

Associated revisions

Revision e294f4cf
Added by nobu (Nobuyoshi Nakada) over 3 years ago

not overwrite cause

  • eval.c (setup_exception): set the cause only if it is explicitly given or not set yet. [Bug #12068]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53819 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 53819
Added by nobu (Nobuyoshi Nakada) over 3 years ago

not overwrite cause

  • eval.c (setup_exception): set the cause only if it is explicitly given or not set yet. [Bug #12068]

Revision 53819
Added by nobu (Nobuyoshi Nakada) over 3 years ago

not overwrite cause

  • eval.c (setup_exception): set the cause only if it is explicitly given or not set yet. [Bug #12068]

Revision 53819
Added by nobu (Nobuyoshi Nakada) over 3 years ago

not overwrite cause

  • eval.c (setup_exception): set the cause only if it is explicitly given or not set yet. [Bug #12068]

Revision 53819
Added by nobu (Nobuyoshi Nakada) over 3 years ago

not overwrite cause

  • eval.c (setup_exception): set the cause only if it is explicitly given or not set yet. [Bug #12068]

Revision 53298a28
Added by nagachika (Tomoyuki Chikanaga) about 3 years ago

merge revision(s) 53819,53822: [Backport #12068]

    * eval.c (setup_exception): set the cause only if it is explicitly
      given or not set yet.  [Bug #12068]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@54617 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 54617
Added by nagachika (Tomoyuki Chikanaga) about 3 years ago

merge revision(s) 53819,53822: [Backport #12068]

* eval.c (setup_exception): set the cause only if it is explicitly
  given or not set yet.  [Bug #12068]

Revision 22a97cbf
Added by usa (Usaku NAKAMURA) about 3 years ago

merge revision(s) 53819,53822: [Backport #12068]

    * eval.c (setup_exception): set the cause only if it is explicitly
      given or not set yet.  [Bug #12068]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@54675 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 54675
Added by usa (Usaku NAKAMURA) about 3 years ago

merge revision(s) 53819,53822: [Backport #12068]

* eval.c (setup_exception): set the cause only if it is explicitly
  given or not set yet.  [Bug #12068]

History

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

In JRuby, cause is set just in initialize?

$ jruby -v -e 'raise "a" rescue p RuntimeError.new("b").cause'
jruby 9.1.0.0-SNAPSHOT (2.2.3) 2016-02-14 c08ca7a Java HotSpot(TM) 64-Bit Server VM 25.25-b02 on 1.8.0_25-b17 +jit [darwin-x86_64]
#<RuntimeError: a>
$ ruby -v -e 'raise "a" rescue p RuntimeError.new("b").cause'
ruby 2.4.0dev (2016-02-13 trunk 53817) [universal.x86_64-darwin15]
nil

Updated by headius (Charles Nutter) over 3 years ago

Yes...the exception inherits current $! only in initialize, and carries it until overwritten.

#3

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

  • Status changed from Open to Closed

Applied in changeset r53819.


not overwrite cause

  • eval.c (setup_exception): set the cause only if it is explicitly given or not set yet. [Bug #12068]

Updated by headius (Charles Nutter) over 3 years ago

May I backport this change to 2.3? Matz? I believe the original behavior was unintentional and should be fixed.

Updated by nagachika (Tomoyuki Chikanaga) over 3 years ago

  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.0.0: DONTNEED, 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED

Please don't backport to the stable branches by yourself. The branch maintainers (usa and me) will do.

I fill Backport field.
I don't think the existing application depend on this behavior. Any opinions?

Updated by nagachika (Tomoyuki Chikanaga) about 3 years ago

  • Backport changed from 2.0.0: DONTNEED, 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED to 2.0.0: DONTNEED, 2.1: REQUIRED, 2.2: REQUIRED, 2.3: DONE

ruby_2_3 r54617 merged revision(s) 53819,53822.

Updated by usa (Usaku NAKAMURA) about 3 years ago

  • Backport changed from 2.0.0: DONTNEED, 2.1: REQUIRED, 2.2: REQUIRED, 2.3: DONE to 2.0.0: DONTNEED, 2.1: REQUIRED, 2.2: DONE, 2.3: DONE

ruby_2_2 r54675 merged revision(s) 53819,53822.

Also available in: Atom PDF