X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 78.192.65.63 Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!proxad.net!feeder1-2.proxad.net!nntpfeed.proxad.net!news.muarf.org!news.ecp.fr!jussieu.fr!not-for-mail From: Alain Ketterlin Newsgroups: comp.lang.python Subject: Re: fork/exec & close file descriptors Date: Tue, 02 Jun 2015 17:59:51 +0200 Organization: =?utf-8?Q?Universit=C3=A9?= de Strasbourg Lines: 32 Message-ID: <87pp5eksnc.fsf@universite-de-strasbourg.fr.invalid> References: NNTP-Posting-Host: 130.79.192.244 Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: vishnu.jussieu.fr 1433260791 52025 130.79.192.244 (2 Jun 2015 15:59:51 GMT) X-Complaints-To: usenet@vishnu.jussieu.fr NNTP-Posting-Date: Tue, 2 Jun 2015 15:59:51 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:cPGnkfal7F8dXGkXe6KjAiVGvNM= Xref: csiph.com comp.lang.python:91853 Skip Montanaro writes: > Reviving (and concluding) a thread I started a couple weeks ago, I asked: > >> 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? > > I wanted this again today (for different reasons than before). > Googling for "python close all file descriptors" returned the os > module docs as the first hit, and lo and behold, what do I see > documented? os.closerange (new in 2.6): > > os.closerange(fd_low, fd_high) > Close all file descriptors from fd_low (inclusive) to fd_high > (exclusive), ignoring errors. The close(2) manpage has the following warning on my Linux system: | Not checking the return value of close() is a common but nevertheless | serious programming error. It is quite possible that errors on a pre‐ | vious write(2) operation are first reported at the final close(). Not | checking the return value when closing the file may lead to silent loss | of data. This can especially be observed with NFS and with disk quota. | (I haven't followed the thread, but if your problem is to make sure fds are closed on exec, you may be better off using the... close-on-exec flag. Or simply do the bookkeeping.) -- Alain.