Project

General

Profile

Actions

Bug #21991

open

`$!` stays as the first exception in Ruby Box

Bug #21991: `$!` stays as the first exception in Ruby Box

Added by tikkss (Tsutomu Katsube) about 13 hours ago. Updated about 4 hours ago.

Status:
Open
Target version:
-
ruby -v:
ruby 4.0.2 (2026-03-17 revision d3da9fec82) +PRISM [x86_64-darwin24]
[ruby-core:125235]

Description

Description

When Ruby Box is enabled (RUBY_BOX=1), $! inside rescue does not change after the first exception.

$! is expected to show the exception for each rescue block, but it always shows the first one.

Steps to reproduce

# test.rb
begin
  raise "First error"
rescue
  pp $!
end

begin
  raise "Second error"
rescue
  pp $!
end

begin
  raise "Third error"
rescue
  pp $!
end

Expected result

$ RUBY_BOX=1 ruby test.rb
ruby: warning: Ruby::Box is experimental, and the behavior may change in the future!
See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc.
#<RuntimeError: First error>
#<RuntimeError: Second error>
#<RuntimeError: Third error>

Actual result

$ RUBY_BOX=1 ruby test.rb
ruby: warning: Ruby::Box is experimental, and the behavior may change in the future!
See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc.
#<RuntimeError: First error>
#<RuntimeError: First error>
#<RuntimeError: First error>

Additional information

This issue does not reproduce when RUBY_BOX=1 is not set:

$ ruby test.rb
#<RuntimeError: First error>
#<RuntimeError: Second error>
#<RuntimeError: Third error>

Also, this issue does not reproduce when the exception object is captured explicitly in the rescue clause:

# test.rb
begin
  raise "First error"
rescue => e
  pp e
end

begin
  raise "Second error"
rescue => e
  pp e
end

begin
  raise "Third error"
rescue => e
  pp e
end
$ RUBY_BOX=1 ruby test.rb
ruby: warning: Ruby::Box is experimental, and the behavior may change in the future!
See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc.
#<RuntimeError: First error>
#<RuntimeError: Second error>
#<RuntimeError: Third error>

Related issues 1 (1 open0 closed)

Related to Ruby - Bug #21823: $! is nil inside rescue block when $! is accessed before raise in Ruby::BoxOpentagomoris (Satoshi Tagomori)Actions

Updated by byroot (Jean Boussier) about 4 hours ago Actions #1

  • Related to Bug #21823: $! is nil inside rescue block when $! is accessed before raise in Ruby::Box added

Updated by byroot (Jean Boussier) about 4 hours ago Actions #2

  • Assignee set to tagomoris (Satoshi Tagomori)
Actions

Also available in: PDF Atom