Backport #5218

at_exit bug with exception handling

Added by Edgars Beigarts over 2 years ago. Updated about 1 year ago.

[ruby-core:39063]
Status:Closed
Priority:Normal
Assignee:Usaku NAKAMURA

Description

It seems that ruby 1.9.2 and later doesn't like exception handling in at_exit blocks and resets the exit code to 0.

Here is an example:

#!/usr/bin/ruby

at_exit do
  raise "X" rescue nil
end

at_exit do
  nil
end

at_exit do
  exit 1
end

at_exit do
  exit 2
end

Results:

### ruby 1.9.4dev (2011-08-23 trunk 33027) [x86_64-darwin10.8.0]

$ ruby at_exit.rb ; echo $?
0

### ruby 1.9.3dev (2011-07-31 revision 32789) [x86_64-darwin10.8.0]

$ ruby at_exit.rb ; echo $?
0

### ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

$ ruby at_exit.rb ; echo $?
0

### ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin10.8.0]

$ ruby at_exit.rb ; echo $?
1

### jruby 1.6.3 (ruby-1.8.7-p330) (2011-07-07 965162f) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_26) [darwin-x86_64-java]

$ jruby at_exit.rb ; echo $?
1

### jruby 1.6.3 (ruby-1.9.2-p136) (2011-07-07 965162f) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_26) [darwin-x86_64-java]

$ jruby --1.9 at_exit.rb ; echo $?
1

Related issues:

https://github.com/jnicklas/capybara/pull/463
https://github.com/jnicklas/capybara/issues/178
https://github.com/rspec/rspec-core/pull/410

Associated revisions

Revision 39378
Added by Usaku NAKAMURA about 1 year ago

merge revision(s) 34982: [Backport #5218]

* eval_jump.c (rb_exec_end_proc): remember the latest exit status.
  [Bug #5218]

History

#1 Updated by Edgars Beigarts over 2 years ago

To fix this I came up with this workaround:

if defined?(RUBYENGINE) && RUBYENGINE == "ruby" && RUBYVERSION >= "1.9"
module Kernel
alias :
atexit :atexit
def at
exit(&block)
_atexit do
exitstatus = $!.status if $!.isa?(SystemExit)
block.call
exit exitstatus if exitstatus
end
end
end
end

#2 Updated by Koichi Sasada about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Nobuyoshi Nakada

#3 Updated by Nobuyoshi Nakada about 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r34982.
Edgars, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • evaljump.c (rbexecendproc): remember the latest exit status. [Bug #5218]

#4 Updated by Daniel Harrington over 1 year ago

Hi guys,

thank you for this fix!
Any information about when it's going to be released?

Cheers,
Daniel

#5 Updated by Michiel de Mare about 1 year ago

So what happened to the changeset? It's still broken in 1.9.3-p385.

#6 Updated by Nobuyoshi Nakada about 1 year ago

  • Tracker changed from Bug to Backport
  • Project changed from ruby-trunk to Backport93
  • Status changed from Closed to Assigned
  • Assignee changed from Nobuyoshi Nakada to Usaku NAKAMURA

#7 Updated by Usaku NAKAMURA about 1 year ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r39378.
Edgars, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


merge revision(s) 34982: [Backport #5218]

* eval_jump.c (rb_exec_end_proc): remember the latest exit status.
  [Bug #5218]

Also available in: Atom PDF