fork/exec & close file descriptors

Ethan Furman ethan at stoneleaf.us
Tue May 19 12:35:58 EDT 2015


On 05/19/2015 05:59 AM, Skip Montanaro wrote:

> Due to presumed bugs in an underlying library over which I have no control, I'm considering a restart in the wee hours of the morning. The basic fork/exec dance is not a problem, but how do I discover
> all the open file descriptors in the new child process to make sure they get closed? Do I simply start at fd 3 and call os.close() on everything up to some largish fd number? Some of these file
> descriptors will have been opened by stuff well below the Python level, so I don't know them a priori.

Pandaemonium [1] (and I believe Ben Finney's daemon [2]) use something akin to the following:

def close_open_files(exclude):
     max_files = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
     keep = set()
     for file in exclude:
         if isinstance(file, baseint):
             keep.add(file)
         elif hasattr(file, 'fileno'):
             keep.add(file.fileno())
         else:
             raise ValueError(
                     'files to not close should be either an file descriptor, '
                     'or a file-type object, not %r (%s)' % (type(file), file))
     for fd in range(max_files, -1, -1):
         if fd in keep:
             continue
         try:
             os.close(fd)
         except OSError:
             exc = sys.exc_info()[1]
             if exc.errno == errno.EBADF:
                 continue
             raise

So, yeah, basically a brute-force method.

--
~Ethan~


[1] https://pypi.python.org/pypi/pandaemonium
[2] https://pypi.python.org/pypi/python-daemon



More information about the Python-list mailing list