@@ -547,6 +547,22 @@ def _call_connection_lost(self, exc):
547547 self ._loop = None
548548
549549
550+ if hasattr (os , 'set_inheritable' ):
551+ # Python 3.4 and newer
552+ _set_inheritable = os .set_inheritable
553+ else :
554+ import fcntl
555+
556+ def _set_inheritable (fd , inheritable ):
557+ cloexec_flag = getattr (fcntl , 'FD_CLOEXEC' , 1 )
558+
559+ old = fcntl .fcntl (fd , fcntl .F_GETFD )
560+ if not inheritable :
561+ fcntl .fcntl (fd , fcntl .F_SETFD , old | cloexec_flag )
562+ else :
563+ fcntl .fcntl (fd , fcntl .F_SETFD , old & ~ cloexec_flag )
564+
565+
550566class _UnixSubprocessTransport (base_subprocess .BaseSubprocessTransport ):
551567
552568 def _start (self , args , shell , stdin , stdout , stderr , bufsize , ** kwargs ):
@@ -558,6 +574,12 @@ def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
558574 # other end). Notably this is needed on AIX, and works
559575 # just fine on other platforms.
560576 stdin , stdin_w = self ._loop ._socketpair ()
577+
578+ # Mark the write end of the stdin pipe as non-inheritable,
579+ # needed by close_fds=False on Python 3.3 and older
580+ # (Python 3.4 implements the PEP 446, socketpair returns
581+ # non-inheritable sockets)
582+ _set_inheritable (stdin_w .fileno (), False )
561583 self ._proc = subprocess .Popen (
562584 args , shell = shell , stdin = stdin , stdout = stdout , stderr = stderr ,
563585 universal_newlines = False , bufsize = bufsize , ** kwargs )
0 commit comments