Project

General

Profile

Actions

Bug #4400

closed

nested at_exit hooks run in strange order

Added by sunaku (Suraj Kurapati) over 11 years ago. Updated about 11 years ago.

Status:
Closed
Priority:
Normal
Target version:
ruby -v:
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
Backport:
[ruby-core:35237]

Description

=begin
Hello,

The documentation for Kernel#at_exit says "If multiple [at_exit] handlers are
registered, they are executed in reverse order of registration". However, does
not seem to be true for nested at_exit hooks (registering an at_exit hook inside
another at_exit hook). For example consider this code:

at_exit { puts :outer0 }
at_exit { puts :outer1_begin; at_exit { puts :inner1 }; puts :outer1_end }
at_exit { puts :outer2_begin; at_exit { puts :inner2 }; puts :outer2_end }
at_exit { puts :outer3 }

Here is the output of running this code with two Rubies:

ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
outer3
outer2_begin
outer2_end
outer1_begin
outer1_end
outer0
inner1
inner2

ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
outer3
outer2_begin
outer2_end
outer1_begin
outer1_end
outer0
inner1
inner2

Observe how inner1 and inner2 are executed in registration order after all
non-nested hooks are executed in reverse registration order. This seems very
strange to me; I would expect nested at_exit hooks to be executed immediately
(as follows) because we are already inside the at_exit phase of the program:

outer3
outer2_begin
inner2
outer2_end
outer1_begin
inner1
outer1_end
outer0

What do you think? Thanks for your consideration.
=end


Files

bug4400-atexit.patch (1.99 KB) bug4400-atexit.patch kosaki (Motohiro KOSAKI), 02/15/2011 08:25 PM
Actions

Also available in: Atom PDF