In general Java exceptions should not have themselves as cause and JRuby in most cases handles this correctly.
We just ran into a problem where an ArgumentError raised due to an encoding problem does have itself as cause which leads to a problem in an ActiveSupport method.
The ActiveSupport method is a little dangerous/questionable by itself, but the recursive cause should be prevented in JRuby nonetheless and there is also a test that checks this for RuntimeErrors.
We're not sure if this is the most general reproduction, so feel free to ignore this PR if you find a more general test.
Show that ArgumentError from encoding error is recursive
Issue #4024 might be related to this.
I think this is simply us being too eager to use the in-flight exception when re-raising, not realizing we're stuffing the same exception into itself again. Looking.
This only appears to affect re-raising using the raise ex form, and I have a fix coming.
$ jruby -e 'begin; "hi \255".split; rescue => e; p e.cause; raise e; end rescue p $!.cause'
#<ArgumentError: invalid byte sequence in UTF-8>
I've fixed the issue, but the test would be better going into ruby/spec (spec/ruby/core/exception or spec/ruby/core/kernel/raise_spec.rb).