[Python-Dev] PEP 446: Open issues/questions

Richard Oudkerk shibturn at gmail.com
Fri Aug 2 10:02:00 CEST 2013

On 02/08/2013 7:44am, Charles-François Natali wrote:
> Then how about changing the default to creating file descriptors
> unheritable on Windows (which is apparently the default)?
> Then you can implement keep_fds by setting them inheritable right
> before creation, and resetting them right after: sure there's a race
> in a multi-threaded program, but AFAICT that's already the case right
> now, and Windows API doesn't leave us any other choice.
> Amusingly, they address this case by recommending putting process
> creation in a critical section:
> http://support.microsoft.com/kb/315939/en-us
> This way, we keep default platform behavior on Unix and on Windows (so
> user using low-level syscalls/APIs won't be surprised), and we have a
> clean way to selectively inherit FD in child processes through
> subprocess.

http://bugs.python.org/issue16500 is a proposal/patch for adding atfork. 
  But it also has a public recursive lock which is held when starting 
processes using fork()/subprocess/multiprocessing.  This is so that 
users can safely manipulate fds while holding the lock, without these 
sorts of race conditions.  For example

     with atfork.getlock():
         fd = os.open("somefile", os.O_CREAT | os.O_WRONLY, 0600)
         flags = fcntl.fcntl(fd, fcntl.F_GETFL)
         fcntl.fcntl(fd, fcntl.F_SETFL, flags | fcntl.FD_CLOEXEC)


More information about the Python-Dev mailing list