]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-29704: Fix asyncio.SubprocessStreamProtocol closing (#405)
authorSeth M. Larson <SethMichaelLarson@users.noreply.github.com>
Fri, 3 Mar 2017 04:21:18 +0000 (22:21 -0600)
committerYury Selivanov <yury@magic.io>
Fri, 3 Mar 2017 17:23:44 +0000 (12:23 -0500)
Lib/asyncio/subprocess.py
Lib/test/test_asyncio/test_subprocess.py
Misc/NEWS

index b2f5304f772121de9c60691a9cc5499cfd07a168..4c85466859f8f0f195d5fd0fc72c269b7cdca2b8 100644 (file)
@@ -24,6 +24,8 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
         self._limit = limit
         self.stdin = self.stdout = self.stderr = None
         self._transport = None
+        self._process_exited = False
+        self._pipe_fds = []
 
     def __repr__(self):
         info = [self.__class__.__name__]
@@ -43,12 +45,14 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
             self.stdout = streams.StreamReader(limit=self._limit,
                                                loop=self._loop)
             self.stdout.set_transport(stdout_transport)
+            self._pipe_fds.append(1)
 
         stderr_transport = transport.get_pipe_transport(2)
         if stderr_transport is not None:
             self.stderr = streams.StreamReader(limit=self._limit,
                                                loop=self._loop)
             self.stderr.set_transport(stderr_transport)
+            self._pipe_fds.append(2)
 
         stdin_transport = transport.get_pipe_transport(0)
         if stdin_transport is not None:
@@ -86,9 +90,18 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
             else:
                 reader.set_exception(exc)
 
+        if fd in self._pipe_fds:
+            self._pipe_fds.remove(fd)
+        self._maybe_close_transport()
+
     def process_exited(self):
-        self._transport.close()
-        self._transport = None
+        self._process_exited = True
+        self._maybe_close_transport()
+
+    def _maybe_close_transport(self):
+        if len(self._pipe_fds) == 0 and self._process_exited:
+            self._transport.close()
+            self._transport = None
 
 
 class Process:
index bba688bb5a53c7eb1f35a861c1ba50f844a4fb9c..2e14a8a9735535719f4d22281b73a1a630f349d7 100644 (file)
@@ -459,6 +459,30 @@ class SubprocessMixin:
                     self.loop.run_until_complete(create)
                 self.assertEqual(warns, [])
 
+    def test_read_stdout_after_process_exit(self):
+        @asyncio.coroutine
+        def execute():
+            code = '\n'.join(['import sys',
+                              'for _ in range(64):',
+                              '    sys.stdout.write("x" * 4096)',
+                              'sys.stdout.flush()',
+                              'sys.exit(1)'])
+
+            fut = asyncio.create_subprocess_exec(
+                sys.executable, '-c', code,
+                stdout=asyncio.subprocess.PIPE,
+                loop=self.loop)
+
+            process = yield from fut
+            while True:
+                data = yield from process.stdout.read(65536)
+                if data:
+                    yield from asyncio.sleep(0.3, loop=self.loop)
+                else:
+                    break
+
+        self.loop.run_until_complete(execute())
+
 
 if sys.platform != 'win32':
     # Unix
index 32741ce1e7d958317484ab8d333c76d2a06afa4c..b17ad90a01398a1e9428308384796192b3c3cfb0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,9 @@ Extension Modules
 Library
 -------
 
+- bpo-29704: asyncio.subprocess.SubprocessStreamProtocol no longer closes before
+  all pipes are closed.
+
 - bpo-29703: Fix asyncio to support instantiation of new event loops
   in child processes.