[Python-Dev] Add "e" (close and exec) mode to open()

Victor Stinner victor.stinner at gmail.com
Tue Jan 8 00:48:15 CET 2013


Hi,

I would like add a new flag to open() mode to close the file on exec:
"e". This feature exists using different APIs depending on the OS and
OS version: O_CLOEXEC, FD_CLOEXEC and O_NOINHERIT. Do you consider
that such flag would be interesting?

On Linux (glibc), the feature is available as the "e" flag for fopen()
(O_CLOEXEC or FD_CLOEXEC depending on the kernel version). With Visual
Studio, it's the "N" flag (O_NOINHERIT).

I would like to modify open() to be able to benefit of O_CLOEXEC
atomicity. The FD_CLOEXEC method has a race condition if another
thread does fork() between open() and fcntl() (two calls to fcntl()
are required).

I created a patch available on the following issue:
http://bugs.python.org/issue16850

Drawbacks of my idea/patch:

- my patch raises NotImplementedError if the platform doesn't support
close-and-exec flag. I tested Linux, OpenBSD, FreeBSD, OpenIndiana
(Solaris) and Mac OS X: my patch works on all these platforms (except
OpenBSD 4.9 because of an OS bug fixed in OpenBSD 5.2). I don't know
platform without this flag.

- my patch raises an error if opener argument of open() is used:
opener and the "e" mode are exclusive (see the issue for details)

---

I first proposed to only expose the atomic close-and-exec option, but
only a few platforms (Linux 2.6.23+, FreeBSD 8.3+, Windows, QNX)
support it. It's more convinient to have the best-effort method (only
atomic if available).

This article of Ulrich Drepper explain which kind of problem are
solved by O_CLOEXEC:
http://udrepper.livejournal.com/20407.html

Victor


More information about the Python-Dev mailing list