Skip to content

Commit ddf0f1f

Browse files
andrykonchinfniephaus
authored andcommitted
[GR-18163] Fix "circular causes" error at re-raising exception when it's caused by implicitly raised exception
PullRequest: truffleruby/4553
2 parents ff1874a + 64450ca commit ddf0f1f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

spec/ruby/core/kernel/raise_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@
240240
end
241241
end
242242

243+
it "re-raises a previously rescued exception that doesn't have a cause and is a cause of other exception (that wasn't raised explicitly) without setting a cause implicitly" do
244+
begin
245+
begin
246+
raise "Error 1"
247+
rescue => e1
248+
begin
249+
foo # raises NameError
250+
rescue => e2
251+
e1.cause.should == nil
252+
e2.cause.should == e1
253+
raise e1
254+
end
255+
end
256+
rescue => e
257+
e.should == e1
258+
e.cause.should == nil
259+
end
260+
end
261+
243262
it "re-raises a previously rescued exception that has a cause but isn't a cause of any other exception without setting a cause implicitly" do
244263
begin
245264
begin

src/main/java/org/truffleruby/core/exception/RubyException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class RubyException extends RubyDynamicObject implements ObjectGraphNode
5050
public Object backtraceLocations = null;
5151
/** null (not set), RubyArray of Strings, or nil (empty) */
5252
public Object customBacktrace = null;
53+
/** whether a Ruby exception referencing this exception as its cause exists. One of the way to prevent circles in
54+
* exception cause chains */
5355
public boolean usedAsACause = false;
5456

5557
public RubyException(RubyClass rubyClass, Shape shape, Object message, Backtrace backtrace, Object cause) {
@@ -58,6 +60,10 @@ public RubyException(RubyClass rubyClass, Shape shape, Object message, Backtrace
5860
this.message = message;
5961
this.backtrace = backtrace;
6062
this.cause = cause;
63+
64+
if (cause instanceof RubyException) {
65+
((RubyException) cause).usedAsACause = true;
66+
}
6167
}
6268

6369
@TruffleBoundary

0 commit comments

Comments
 (0)