Skip to content

exit with a delegator raises a Java ClassCastException, and SystemExit.new(obj) ignores obj.to_int #10106

Open

Description

@taxor03

Two problems with an exit status that is not an Integer:

  • exit(obj) (also exit! and Process.exit) raises a Java ClassCastException when obj is a BasicObject that is not an Object, such as a SimpleDelegator. 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 raises TypeError.
  • SystemExit.new(obj) with an obj that responds to to_int gives status 0 and uses obj as the message. exit(obj) with the same object converts it on JRuby, and CRuby's SystemExit.new converts 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 with RubyObject argument = (RubyObject) args[0]; (RubyKernel.java#L941; on master Helpers.exit, Helpers.java#L785). The rest of the method only needs an IRubyObject; CRuby's exit_status_code takes true, false, or NUM2INT(status) (process.c#L4341).
  • RubySystemExit#initialize takes the first argument as the status only when it is a RubyFixnum or a boolean, and otherwise as the message (RubySystemExit.java#L93-L101; the same lines on master). CRuby's exit_initialize tries rb_check_to_int(status) first and uses the argument as the message only when that returns nil (error.c#L2244).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions