Roger Pack wrote:
> This is very interesting.  I may have run into something similar to it
> before.  Do you have an example of it?

You mean an example of the problem? I can't seem to be able to come up 
with a small, isolated code sample that shows the problem, but I do know 
that if I comment out the fix (closing IO objects), applications will 
raise Errno::EBADFD on some (but not all) platform and with certain 
versions of Rails.

> Why do you close all file descriptors after fork?

Phusion Passenger's spawn manager is connected to Apache through an 
anonymous Unix socket. This allows Apache to send spawn requests to the 
spawn manager. Rails application processes spawned by the spawn manager 
must not have this file descriptor open, because leaving it open is a 
hint to Apache that the spawn manager is still alive. If the spawn 
manager crashed but a spawned application still has the communication 
socket open, then Apache will not detect that and won't restart the 
spawn manager.
Initially, I implemented a white list of file descriptors and IO objects 
to close upon fork. This turned out to be very error-prone: it's very 
easy to forget closing a file descriptor/IO object, which can lead to 
severe but hard-to-debug resource leaks. Plus, the spawn manager loads 
arbitrary libraries upon loading a Rails app, and one never knows what 
kind of file descriptors those libraries may open. A blacklist is 
totally necessary for proper stability.