Repository navigation
Open
Description
Two problems with an exit status that is not an Integer:
exit(obj)(alsoexit!andProcess.exit) raises a JavaClassCastExceptionwhenobjis aBasicObjectthat is not anObject, such as aSimpleDelegator. CRuby converts it like any other status:exit(SimpleDelegator.new(3))exits with status 3, and a delegator of something that is not an Integer raisesTypeError.SystemExit.new(obj)with anobjthat responds toto_intgives status 0 and usesobjas the message.exit(obj)with the same object converts it on JRuby, and CRuby'sSystemExit.newconverts it too. The docs say "Status is true, false, or an integer" (SystemExit.new).
require 'delegate'
status = Object.new
def status.to_int = 3
begin
exit(status)
rescue SystemExit => e
p [e.status, e.message]
end
e = SystemExit.new(status)
p [e.status, e.message]
exit(SimpleDelegator.new(3))CRuby 4.0.9 (ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [aarch64-linux]), and 3.4.12 and 3.3.13 print the same:
$ ruby exit.rb; echo "exit status $?"
[3, "exit"]
[3, "SystemExit"]
exit status 3
JRuby 10.0.8.0 (jruby 10.0.8.0 (3.4.12) 2026-11-05 ef393d4e54 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]):
$ jruby exit.rb; echo "exit status $?"
[3, "exit"]
[0, "#<Object:0x73cd37c0>"]
Unhandled Java exception: java.lang.ClassCastException: class org.jruby.RubyBasicObject cannot be cast to class org.jruby.RubyObject (org.jruby.RubyBasicObject and org.jruby.RubyObject are in module org.jruby.dist of loader 'app')
java.lang.ClassCastException: class org.jruby.RubyBasicObject cannot be cast to class org.jruby.RubyObject (org.jruby.RubyBasicObject and org.jruby.RubyObject are in module org.jruby.dist of loader 'app')
exit at org/jruby/RubyKernel.java:954
exit at org/jruby/RubyKernel.java:934
...
exit status 1
JRuby 10.1.3.0 (jruby 10.1.3.0 (4.0.9) 2026-11-05 3db52d02f9 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]) and a build of master (54dd973) print the same, with the cast in org/jruby/runtime/Helpers.java:785.
- The private
RubyKernel.exit(context, args, hard)starts withRubyObject argument = (RubyObject) args[0];(RubyKernel.java#L941; on masterHelpers.exit, Helpers.java#L785). The rest of the method only needs anIRubyObject; CRuby'sexit_status_codetakestrue,false, orNUM2INT(status)(process.c#L4341). RubySystemExit#initializetakes the first argument as the status only when it is aRubyFixnumor a boolean, and otherwise as the message (RubySystemExit.java#L93-L101; the same lines on master). CRuby'sexit_initializetriesrb_check_to_int(status)first and uses the argument as the message only when that returns nil (error.c#L2244).
Metadata
Metadata
Assignees
Labels
No labels
Activity