New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`Exception#cause` is difference behavior with CRuby 2.3.0 #3654

Closed
hsbt opened this Issue Feb 9, 2016 · 4 comments

Comments

Projects
None yet
3 participants
@hsbt

hsbt commented Feb 9, 2016

I found difference behavior of Exception#cause with CRuby 2.3.0. I invoke following codes. There are part of Rake test code.

begin
  raise 'cause a'
rescue
  begin
    raise 'cause b'
  rescue => ex
    p [ex, ex.cause]
  end
end

begin
  begin
    raise 'cause a'
  rescue => a
    begin
      raise 'cause b'
    rescue
      raise a
    end
  end
rescue => ex
  p [ex, ex.cause]
end

I got following results.

% ruby -v cause.rb
jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 25.72-b15 on 1.8.0_72-b15 +jit [darwin-x86_64]
[#<RuntimeError: cause b>, #<RuntimeError: cause a>]
[#<RuntimeError: cause a>, nil]

% ruby -v cause.rb
ruby 2.3.0p5 (2016-01-15 revision 53536) [x86_64-darwin15]
[#<RuntimeError: cause b>, #<RuntimeError: cause a>]
[#<RuntimeError: cause a>, #<RuntimeError: cause b>]

Is JRuby results intentional behavior? If it's yes, I will fix rake tests.

@enebo enebo added this to the JRuby 9.1.0.0 milestone Feb 9, 2016

@enebo enebo added the ir label Feb 9, 2016

@enebo

This comment has been minimized.

Show comment
Hide comment
@enebo

enebo Feb 9, 2016

Member

@hsbt I would say we are just doing the wrong thing here. We will fix it.

Member

enebo commented Feb 9, 2016

@hsbt I would say we are just doing the wrong thing here. We will fix it.

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Feb 13, 2016

Member

I'm not sure this is correct behavior in MRI, and I'll explain why.

In the differing case, a is not caused by b. They appear to be setting cause every time the exception is raised, which I'd consider a bug...since it would lose the original cause. An example of where this is broken:

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

And the output in JRuby 9.0.5.0 and MRI 2.3.0:

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

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

MRI is losing the original cause of the exception, without it being physically replaced.

I'm going to report this as a bug.

Interestingly, JRuby 9.1 (ruby-2.3 branch) appears to work like Ruby 2.3, so somewhere along the way we "fixed" this:

[] ~/projects/jruby $ jruby -v cause.rb
jruby 9.1.0.0-SNAPSHOT (2.3.0) 2016-02-13 8136dd9 Java HotSpot(TM) 64-Bit Server VM 25.60-b23 on 1.8.0_60-b27 +jit [darwin-x86_64]
[#<RuntimeError: b>, #<RuntimeError: a>]
[#<RuntimeError: b>, #<RuntimeError: c>]
Member

headius commented Feb 13, 2016

I'm not sure this is correct behavior in MRI, and I'll explain why.

In the differing case, a is not caused by b. They appear to be setting cause every time the exception is raised, which I'd consider a bug...since it would lose the original cause. An example of where this is broken:

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

And the output in JRuby 9.0.5.0 and MRI 2.3.0:

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

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

MRI is losing the original cause of the exception, without it being physically replaced.

I'm going to report this as a bug.

Interestingly, JRuby 9.1 (ruby-2.3 branch) appears to work like Ruby 2.3, so somewhere along the way we "fixed" this:

[] ~/projects/jruby $ jruby -v cause.rb
jruby 9.1.0.0-SNAPSHOT (2.3.0) 2016-02-13 8136dd9 Java HotSpot(TM) 64-Bit Server VM 25.60-b23 on 1.8.0_60-b27 +jit [darwin-x86_64]
[#<RuntimeError: b>, #<RuntimeError: a>]
[#<RuntimeError: b>, #<RuntimeError: c>]
@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Feb 13, 2016

Member

Filed https://bugs.ruby-lang.org/issues/12068

If they disagree (and I will argue the point) then I guess we've already fixed this. If they agree, we'll need to reinstate 9.0.5.0's behavior.

Member

headius commented Feb 13, 2016

Filed https://bugs.ruby-lang.org/issues/12068

If they disagree (and I will argue the point) then I guess we've already fixed this. If they agree, we'll need to reinstate 9.0.5.0's behavior.

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Feb 14, 2016

Member

They agreed, so we need to fix 9.1 to work like 9.0 did.

Member

headius commented Feb 14, 2016

They agreed, so we need to fix 9.1 to work like 9.0 did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment