Skip to content

Convert exit statuses that are not Objects, and SystemExit.new statuses, with to_int - #10113

Open
taxor03 wants to merge 1 commit into
jruby:masterfrom
taxor03:fix-exit-status-conversion-master
Open

taxor03 wants to merge 1 commit into
jruby:masterfrom
taxor03:fix-exit-status-conversion-master

Conversation

@taxor03

@taxor03 taxor03 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10106 on master.

  • exit, exit! and Process.exit cast their argument to RubyObject in Helpers.exit before converting it, so a BasicObject status that is not an Object, such as a SimpleDelegator, raised a Java ClassCastException. The argument is now kept as an IRubyObject; the rest of the method already handled one. As with CRuby's exit_status_code (true, false or NUM2INT(status), process.c#L4341), exit(SimpleDelegator.new(3)) now exits with status 3, and a delegator of something that is not an Integer raises TypeError.
  • SystemExit#initialize took the first argument as the status only when it was a Fixnum or a boolean, and otherwise as the message. Like exit_initialize, which calls rb_check_to_int(status) (error.c#L2244), it is now the status when it is an Integer or to_int returns one, and the message otherwise. So SystemExit.new(obj) with obj.to_int returning 3 has status 3 and message "SystemExit". rb_check_to_int returns nil when to_int returns something else, so this calls to_int without the type check of Convert.checkToInteger, which would raise TypeError there. With the same change, SystemExit.new(3.7) has status 3 and SystemExit.new(2**70) keeps the Bignum as its status, as in CRuby 4.0.9 and 3.4.12; before, both were taken as the message with status 0.

This adds "converts an argument that is not an Object with #to_int" to the shared process_exit and process_exit! specs in spec/ruby/shared/process/exit.rb (run for Kernel#exit, Kernel.exit, Process.exit and the exit! variants), and "accepts a status that converts with #to_int" and "uses the argument as the message if #to_int does not return an Integer" to spec/ruby/core/exception/system_exit_spec.rb. The new examples fail before this change and pass after it, with the default parser and with -Xparser.prism, and pass on CRuby 4.0.9 and 3.4.12. Tested with spec/ruby/core/exception, spec/ruby/core/kernel/exit_spec.rb and spec/ruby/core/process/exit_spec.rb (default parser and -Xparser.prism) and test/mri/ruby/test_exception.rb. No new failures.

This is the master version of #10112, where exit is in Helpers.exit. The new example in shared/process/exit.rb uses should.raise like the rest of that file on master.

@kares

kares commented Nov 18, 2026

Copy link
Copy Markdown
Member

I think jruby-10.0 might still be merged over to master. so the fix from #10112 should make it to master as well.

by the way, is this an actual issue you've stumbled upon with some ruby/gems or more of just a compatibility concern find using LLMs? (can not tell from #10106)

@taxor03

taxor03 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for looking. I opened #10113 because the merge alone won't fix master: there exit has moved from RubyKernel to Helpers.exit, so the (RubyObject) cast that #10112 removes is at Helpers.java:785 on master, and the RubyKernel.java hunk from jruby-10.0 conflicts. #10113 is the same change applied to Helpers.exit, with the new spec in the .should.raise style used on master. If you'd rather port it while merging, I'm happy to close it.

As for how I found it: not with an LLM. I'm doing differential testing of JRuby against CRuby, and I report a mismatch once I've checked it against CRuby's source and the documented behavior. If reports like this aren't useful to you, or you'd prefer fewer of them, just let me know.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants