MagicStack / MagicStack/uvloop

`asyncio.create_subprocess_exec`: cannot read `/dev/stdin`

未关闭
#532 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Cython
星标
11.9k
派生
616
PR 合并指标
30 天内没有已合并 PR

描述

  • uvloop version: uvloop==0.17.0
  • Python version: Python 3.10.10
  • Platform: Linux 0af9a1603f81 5.10.76-linuxkit #1 SMP Mon Nov 8 10:21:19 UTC 2021 x86_64 GNU/Linux (in a Docker container)
  • Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: yes
  • Does uvloop behave differently from vanilla asyncio? How?: yes. Vanilla asyncio had a similar issue patched: https://github.com/python/cpython/issues/90522

Processes run with asyncio.create_subprocess_exec fail to read from /dev/stdin with uvloop but not with asyncio

asyncio had a similar issue fixed recently (in January 2022): https://github.com/python/cpython/issues/90522
This was backported to 3.10 and 3.11

I can reproduce this on Linux in a Docker container, with either Mac OS or Linux as hosts
I cannot reproduce this on Mac OS (seems to work fine)

Logs:

# PYTHONASYNCIODEBUG=1 python test.py
cat: /dev/stdin: No such device or address
Traceback (most recent call last):
  File "//test.py", line 16, in <module>
    asyncio.run(f())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "//test.py", line 13, in f
    stdout, _ = await proc.communicate("test".encode("utf-8"))
  File "/usr/local/lib/python3.10/asyncio/subprocess.py", line 195, in communicate
    stdin, stdout, stderr = await tasks.gather(stdin, stdout, stderr)
  File "/usr/local/lib/python3.10/asyncio/subprocess.py", line 147, in _feed_stdin
    self.stdin.write(input)
  File "/usr/local/lib/python3.10/asyncio/streams.py", line 325, in write
    self._transport.write(data)
  File "uvloop/handles/stream.pyx", line 674, in uvloop.loop.UVStream.write
  File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
RuntimeError: unable to perform operation on <WriteUnixTransport closed=True reading=False 0x7f64e3186dc0>; the handler is closed

Expected (using asyncio):

# PYTHONASYNCIODEBUG=1 python test.py
b'test'

Code to reproduce:

import asyncio

# Commenting out `uvloop` below fixes the issue
import uvloop
uvloop.install()

async def f():
    proc = await asyncio.create_subprocess_exec(
        "cat",
        "/dev/stdin",
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE,
    )
    stdout, _ = await proc.communicate("test".encode("utf-8"))
    print(stdout)

asyncio.run(f())

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 test.py 中的 reproducer 开始,并在 Docker 中的 Linux 上将其行为与 vanilla asyncio 进行比较。跟踪 subprocess 的 stdin 处理流程,检查 uvloop/loop.pyx、uvloop/handles/stream.pyx 和 uvloop/handles/handle.pyx。完成的标准是:/dev/stdin 子进程在 uvloop 下输出 b'test',且不出现 closed-transport error。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend, operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。