# Python 3.11.1: async subprocess stdout lost when piped

**URL:** <https://discuss.python.org/t/python-3-11-1-async-subprocess-stdout-lost-when-piped/21788>\
**Category:** Python Help\
**Tags:** asyncio\
**Created:** [December 9, 2022, 9:21am UTC](https://discuss.python.org/t/python-3-11-1-async-subprocess-stdout-lost-when-piped/21788 "2022-12-09T09:21:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mportesdev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mportesdev/32/10125_2.png) [@mportesdev](https://discuss.python.org/u/mportesdev)\
**Post date:** [December 9, 2022, 9:21am UTC](https://discuss.python.org/t/python-3-11-1-async-subprocess-stdout-lost-when-piped/21788/1 "2022-12-09T09:21:01Z")

</div>

I noticed behavior in Python 3.11.1 that is inconsistent with earlier versions. I’d appreciate if someone tried to reproduce this.

Minimal example:

```python
import asyncio

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

async def main():
    return await asyncio.gather(
        get_command_stdout('echo', 'foo'),
        get_command_stdout('echo', 'bar'),
    )

if __name__ == ' __main__':
    print(asyncio.run(main()))

```

In Python 3.10.9 and 3.11.0 the result is always as I would expect, i.e.

```auto
[b'foo\n', b'bar\n']

```

however in Python 3.11.1 I am constantly getting

```auto
[b'', b'bar\n']

```

I suspect this might be a bug/regression.

All Python versions mentioned above were built with pyenv on Ubuntu 22.04.

---

<div class="post-metadata">

**Author:** ![mportesdev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mportesdev/32/10125_2.png) [@mportesdev](https://discuss.python.org/u/mportesdev)\
**Post date:** [December 9, 2022, 9:49am UTC](https://discuss.python.org/t/python-3-11-1-async-subprocess-stdout-lost-when-piped/21788/2 "2022-12-09T09:49:59Z")

</div>

Additional info:

1. in Python 3.12.0a3 the same problem occurs, i.e. the first command’s stdout is lost in the pipe
2. in all tested Python versions, if the subprocess stdout is not piped, the script’s output is as expected, i.e.

```auto
foo
bar
[None, None]

```

3. in all tested Python versions, if I rewrite the code to synchronous, the result is correct, i.e. `[b'foo\n', b'bar\n']`

---

<div class="post-metadata">

**Author:** ![ronaldoussoren](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ronaldoussoren/32/61_2.png) [@ronaldoussoren](https://discuss.python.org/u/ronaldoussoren)\
**Post date:** [December 9, 2022, 10:52am UTC](https://discuss.python.org/t/python-3-11-1-async-subprocess-stdout-lost-when-piped/21788/3 "2022-12-09T10:52:12Z")

</div>

I can reproduce this on my machine, adding extra commands shows that only the output of the last command is correct.

Could you file an issue about this on [github](https://github.com/python/cpython/issues)?

---

<div class="post-metadata">

**Author:** ![mportesdev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mportesdev/32/10125_2.png) [@mportesdev](https://discuss.python.org/u/mportesdev)\
**Post date:** [December 9, 2022, 12:50pm UTC](https://discuss.python.org/t/python-3-11-1-async-subprocess-stdout-lost-when-piped/21788/4 "2022-12-09T12:50:07Z")

</div>

@ronaldoussoren Thanks for your response. Someone already opened an issue:

> <https://github.com/python/cpython/issues/100133>
>
> \<!--
> If you're new to Python and you're not sure whether what you're experien…cing is a bug, the CPython issue tracker is not
> the right place to seek help. Consider the following options instead:
> 
> - reading the Python tutorial: https://docs.python.org/3/tutorial/
> - posting in the "Users" category on discuss.python.org: https://discuss.python.org/c/users/7
> - emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list
> - searching our issue tracker (https://github.com/python/cpython/issues) to see if
> your problem has already been reported
> \--\>
> 
> \# Bug report
> 
> So, I've updated python from 3.11.0 to 3.11.1 and one of my utilities which runs a lot of external processes with \`asyncio.create\_subprocess\_exec\` started failing in different places in weird ways. It turned out that with some probability \`asyncio.subprocess.Process.communicate()\` would now return an empty stdout. Here's a repro:
> 
> \`\`\`
> import asyncio
> 
> async def main():
> attempt = 1
> while True:
> proc = await asyncio.create\_subprocess\_exec('/bin/echo', 'test', stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
> 
> stdout, stderr = await proc.communicate()
> 
> text = stdout.decode('utf-8').strip()
> 
> if text != 'test':
> raise RuntimeError(f'FAIL on attempt {attempt}: output="{text}"')
> 
> attempt += 1
> 
> asyncio.run(main())
> \`\`\`
> 
> You may have to wait somewhat for the problem to reproduce, but for me it fails in under 15 seconds more or less reliably. Possible output:
> \`\`\`
> RuntimeError: FAIL on attempt 3823: output=""
> \`\`\`
> 
> \# Your environment
> 
> 
> 
> \- CPython versions tested on: 3.11.1
> \- Operating system and architecture: FreeBSD 13.1 amd64
> 
> \<!--
> You can freely edit this text. Remove any lines you believe are unnecessary.
> \--\>
