sbt <shibt...@gmail.com> added the comment:

Attached is a patch (without documentation) which creates an atfork module for 
Unix.

Apart from the atfork() function modelled on pthread_atfork() there is also a 
get_fork_lock() function.  This returns a recursive lock which is held whenever 
a child process is created using os.fork(), subprocess.Popen() or 
multiprocessing.Process().  It can be used like

    with atfork.get_fork_lock():
        r, w = os.pipe()
        pid = os.fork()
        if pid == 0:
            try:
                os.close(r)
                # do something with w
            finally:
                os._exit(0)
        else:
            os.close(w)

    # do something with r

This prevents processes forked by other threads from accidentally inheriting 
the writable end (which would potentially cause EOF to be delayed when reading 
from the pipe).  It can also be used to eliminate the potential race where you 
create an fd and then set the CLOEXEC flag on it.

The patch modifies Popen() and Process.start() to acquire the lock when they 
create their pipes.  (A race condition previously made Process.sentinel and 
Process.join() potentially unreliable in a multithreaded program.)

Note that using the deprecated os.popen?() and os.spawn?() functions can still 
cause accidental inheritance of fds.

(I have also done a hopefully complete patch to multiprocessing to optionally 
allow fork+exec on Unix -- see Issue 8713.)

----------
Added file: http://bugs.python.org/file24303/atfork.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue6721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to