fork/exec & close file descriptors
Alain Ketterlin
alain at universite-de-strasbourg.fr.invalid
Tue Jun 2 11:59:51 EDT 2015
Skip Montanaro <skip.montanaro at gmail.com> 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.
More information about the Python-list
mailing list