Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-100133: fix asyncio subprocess losing stderr and stdout output #100154

Merged
merged 9 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Lib/asyncio/base_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,15 @@ def _process_exited(self, returncode):
# 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()
# See https://github.com/python/cpython/issues/100133
# The pipes should not be closed otherwise some data may be lost.
# If the pipe is closed here then _UnixReadPipeTransport will remove the
# reader prematurely and the data will be lost, instead of doing that
# the pipe will be closed when the process is finished via _pipe_connection_lost
# followed by _try_finish.
# for p in self._pipes.values():
# if p is not None:
# p.pipe.close()
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved

self._try_finish()

Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,23 @@ async def execute():

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)]
gvanrossum marked this conversation as resolved.
Show resolved Hide resolved
res = await asyncio.gather(*[get_command_stdout(sys.executable, '-c',
f'import sys; print({out!r})') for out in outputs])
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(res, outputs)

self.loop.run_until_complete(main())


if sys.platform != 'win32':
# Unix
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in :mod:`asyncio` where a subprocess would sometimes lose data received from pipe. Patch by Kumar Aditya.
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved