# Useful at\_exit idiom

**URL:** <https://www.ruby-forum.com/t/useful-at-exit-idiom/71591>\
**Category:** Ruby\
**Created:** [October 10, 2006, 6:21am UTC](https://www.ruby-forum.com/t/useful-at-exit-idiom/71591 "2006-10-10T06:21:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 10, 2006, 6:21am UTC](https://www.ruby-forum.com/t/useful-at-exit-idiom/71591/1 "2006-10-10T06:21:33Z")

</div>

i’m constantly struggling with the fact that children need to use exit!  
to  
prevent exit handlers registered in the parent being called twice, for  
example:

harp:~ \> cat a.rb  
at\_exit{ puts ‘parent’ }

fork{  
at\_exit{ puts ‘child’ }  
} and Process.wait

harp:~ \> ruby a.rb  
child  
parent  
parent

arggh. this is a neat work-around:

harp:~ \> cat a.rb  
at\_exit{ puts ‘parent’ }

fork{  
at\_exit{ exit! }  
at\_exit{ puts ‘child’ }  
} and Process.wait

harp:~ \> ruby a.rb  
child  
parent

i’m sure i’m not the first one to think of this, but it sure is handy to  
separate the parent’s handlers from the child’s… anyone see issues  
with  
this?

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 10, 2006, 9:13am UTC](https://www.ruby-forum.com/t/useful-at-exit-idiom/71591/2 "2006-10-10T09:13:58Z")

</div>

Hi,

In message “Re: useful at\_exit idiom”  
on Tue, 10 Oct 2006 13:21:21 +0900, removed\_email\_address@domain.invalid writes:  
|

|i’m constantly struggling with the fact that children need to use exit! to  
|prevent exit handlers registered in the parent being called twice,

|i’m sure i’m not the first one to think of this, but it sure is handy to  
|separate the parent’s handlers from the child’s… anyone see issues with  
|this?

linux man atexit(3) says:

```
  When a child process is created via fork(), it inherits copies
  of the its parents registrations. Upon a successful call to one
  of the exec() functions, all registrations are removed.

```

so I think this is expected behavior for most of the cases, since Ruby  
inherits many behaviors from C libraries.

```
						matz.
```
