Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90867 > unrolled thread

Re: fork/exec & close file descriptors

Started byEthan Furman <ethan@stoneleaf.us>
First post2015-05-19 09:35 -0700
Last post2015-05-19 09:35 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: fork/exec & close file descriptors Ethan Furman <ethan@stoneleaf.us> - 2015-05-19 09:35 -0700

#90867 — Re: fork/exec & close file descriptors

FromEthan Furman <ethan@stoneleaf.us>
Date2015-05-19 09:35 -0700
SubjectRe: fork/exec & close file descriptors
Message-ID<mailman.143.1432053377.17265.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web