Skip to content

Instantly share code, notes, and snippets.

@marshalium
Last active August 31, 2017 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshalium/64e9ebb4bb4ac107eee1f5a629feee18 to your computer and use it in GitHub Desktop.
Save marshalium/64e9ebb4bb4ac107eee1f5a629feee18 to your computer and use it in GitHub Desktop.
Reproduce JRuby issue #4699
import java.util.function.Supplier;
public class ExceptionHelper {
private final Throwable error;
public ExceptionHelper(Supplier<Void> errorRaisingSupplier) {
this.error = errorFromSupplier(errorRaisingSupplier);
}
private static Throwable errorFromSupplier(Supplier<Void> errorRaisingSupplier) {
try {
errorRaisingSupplier.get();
} catch (Throwable t) {
return t;
}
throw new RuntimeException("Supplier did not raise an error");
}
public void throwCachedError() throws RuntimeException {
throw new RuntimeException("Wrapper around cached error", this.error);
}
}
#!/usr/bin/env jruby
NUM_THREADS = 5
puts RUBY_DESCRIPTION
class CustomError < StandardError; end
class ErrorRaiser
def initialize
@java_exception_helper = Java::ExceptionHelper.new { raise CustomError }
end
def get
begin
begin
@java_exception_helper.throwCachedError();
rescue Java::JavaLang::RuntimeException => e
raise e.cause
end
rescue CustomError
nil
end
end
end
error_raiser = ErrorRaiser.new
puts "Starting #{NUM_THREADS} threads"
threads = NUM_THREADS.times.map do
Thread.new do
begin
10_000.times do
error_raiser.get
end
puts "#{Thread.current} finished successfully!\n"
rescue => e
puts "#{Thread.current} failed"
puts "#{e.class}: #{e}\n\t#{e.backtrace.join("\n\t")}\n"
end
end
end
puts "Waiting for all threads to finish"
threads.map(&:join)
puts "DONE"
@marshalium
Copy link
Author

Building and running this results in this:

$ javac ExceptionHelper.java && ./bin/jruby java_cause_bug.rb 
jruby 9.1.13.0-SNAPSHOT (2.3.3) 2017-08-31 23f1bb3 Java HotSpot(TM) 64-Bit Server VM 25.121-b13 on 1.8.0_121-b13 +jit [darwin-x86_64]
Starting 5 threads
Waiting for all threads to finish
#<Thread:0x7d0d15c6> failed
Java::JavaLang::IllegalStateException: Can't overwrite cause with java.lang.RuntimeException: Wrapper around cached error
[ ... snip ... ]
#<Thread:0x4a6b6fc3> failed
Java::JavaLang::IllegalStateException: Can't overwrite cause with java.lang.RuntimeException: Wrapper around cached error
[ ... snip ... ]
#<Thread:0x11823d3c> failed
Java::JavaLang::IllegalStateException: Can't overwrite cause with java.lang.RuntimeException: Wrapper around cached error
[ ... snip ... ]
#<Thread:0x3cc378a6> failed
Java::JavaLang::IllegalStateException: Can't overwrite cause with java.lang.RuntimeException: Wrapper around cached error
[ ... snip ... ]
#<Thread:0x2e879dc9> finished successfully!
DONE

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