]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-100133: fix `asyncio` subprocess losing `stderr` and `stdout` output (GH-100154)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 21 Dec 2022 10:24:19 +0000 (02:24 -0800)
committerGitHub <noreply@github.com>
Wed, 21 Dec 2022 10:24:19 +0000 (02:24 -0800)
(cherry picked from commit a7715ccfba5b86ab09f86ec56ac3755c93b46b48)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Lib/asyncio/base_subprocess.py
Lib/test/test_asyncio/test_subprocess.py
Misc/NEWS.d/next/Library/2022-12-10-08-36-07.gh-issue-100133.g-zQlp.rst [new file with mode: 0644]

index e15bb4141fc02ad39b3c73ac0c49c3b9fd2f871f..4c9b0dd5653c0c5478a70b6fa95c28015972b24e 100644 (file)
@@ -215,9 +215,6 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
             # object. On Python 3.6, it is required to avoid a ResourceWarning.
             self._proc.returncode = returncode
         self._call(self._protocol.process_exited)
-        for p in self._pipes.values():
-            if p is not None:
-                p.pipe.close()
 
         self._try_finish()
 
index f71ad72f999ee9931e948cdd117177670c2f4e98..bea2314a528663e110e18f3bf5a05a638a97514e 100644 (file)
@@ -684,6 +684,23 @@ class SubprocessMixin:
 
         self.assertIsNone(self.loop.run_until_complete(execute()))
 
+    def test_subprocess_communicate_stdout(self):
+        # See https://github.com/python/cpython/issues/100133
+        async def get_command_stdout(cmd, *args):
+            proc = await asyncio.create_subprocess_exec(
+                cmd, *args, stdout=asyncio.subprocess.PIPE,
+            )
+            stdout, _ = await proc.communicate()
+            return stdout.decode().strip()
+
+        async def main():
+            outputs = [f'foo{i}' for i in range(10)]
+            res = await asyncio.gather(*[get_command_stdout(sys.executable, '-c',
+                                        f'print({out!r})') for out in outputs])
+            self.assertEqual(res, outputs)
+
+        self.loop.run_until_complete(main())
+
 
 if sys.platform != 'win32':
     # Unix
diff --git a/Misc/NEWS.d/next/Library/2022-12-10-08-36-07.gh-issue-100133.g-zQlp.rst b/Misc/NEWS.d/next/Library/2022-12-10-08-36-07.gh-issue-100133.g-zQlp.rst
new file mode 100644 (file)
index 0000000..881e6ed
--- /dev/null
@@ -0,0 +1 @@
+Fix regression in :mod:`asyncio` where a subprocess would sometimes lose data received from pipe.