Skip to content

mruby-exit: fix SystemExit#status inaccessible from Ruby#6890

Closed
matz wants to merge 1 commit into
masterfrom
fix-mruby-exit-ivsym-status
Closed

mruby-exit: fix SystemExit#status inaccessible from Ruby#6890
matz wants to merge 1 commit into
masterfrom
fix-mruby-exit-ivsym-status

Conversation

@matz

@matz matz commented Jun 13, 2026

Copy link
Copy Markdown
Member

Problem

mruby-exit stores the exit status using MRB_SYM(status) (plain status symbol) instead of MRB_IVSYM(status) (@status ivar symbol). Ruby's instance variable machinery only enumerates and accesses @-prefixed names, so the value is unreachable from Ruby code:

begin
  exit 42
rescue SystemExit => e
  p e.instance_variables  # => []
  p e.instance_variable_get(:@status)  # => nil
end

The internal MRB_EXC_EXIT_STATUS macro in error.h reads with the same wrong symbol, so C consumers (e.g. MRB_EXC_CHECK_EXIT) work correctly — but Ruby callers diverge from CRuby behaviour. All other exception ivars in src/error.c (@name, @args) consistently use MRB_IVSYM.

Fixes #6888.

Changes

  • mruby_exit.c: MRB_SYM(status)MRB_IVSYM(status) at the write site
  • error.h: MRB_EXC_EXIT_STATUS macro updated to match (C callers remain consistent)
  • mrblib/mruby-exit.rb: add SystemExit#status accessor to match CRuby

After

begin
  exit 42
rescue SystemExit => e
  p e.instance_variables              # => [:@status]
  p e.instance_variable_get(:@status) # => 42
  p e.status                          # => 42
end

Generated by Claude Code

MRB_SYM(status) stores the exit status under the plain `status` symbol,
which is invisible to Ruby's ivar machinery. Switch both the write site
in mruby_exit.c and the read macro MRB_EXC_EXIT_STATUS in error.h to
MRB_IVSYM(status) so the value is stored under `@status` and C callers
remain consistent.

Also add SystemExit#status accessor in mrblib to match CRuby behaviour.

https://claude.ai/code/session_018JRpuKQBupCU8iNrvdtrmv

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the SystemExit implementation in mruby by changing the status symbol from MRB_SYM(status) to MRB_IVSYM(status) and introducing a SystemExit#status reader in Ruby. The reviewer suggested implementing a custom initialize method for SystemExit to match CRuby's behavior and ensure @status is correctly initialized when SystemExit is instantiated directly.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +1 to +5
class SystemExit
def status
@status
end
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully match CRuby's behavior and prevent SystemExit#status from returning nil when SystemExit is instantiated directly (e.g., via SystemExit.new), we should implement a custom initialize method that supports the standard arguments: SystemExit.new(status), SystemExit.new(status, message), and SystemExit.new(message). This ensures @status is always initialized to a valid integer (defaulting to 0), matching CRuby's behavior.

class SystemExit
  def initialize(*args)
    status = 0
    message = nil
    if args.size == 1
      if args[0].is_a?(Integer)
        status = args[0]
      else
        message = args[0]
      end
    elsif args.size >= 2
      status = args[0]
      message = args[1]
    end
    if message
      super(message)
    else
      super()
    end
    @status = status
  end

  def status
    @status
  end
end

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SystemExit#status value not accessible from Ruby (mruby-exit gem)

2 participants