MagicStack / MagicStack/uvloop

inconsistent file descriptor blocking state when running in asyncio vs uvloop

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

还没有人认领这个 Issue。

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

描述

When running under uvloop, registering stdin, stdout, or stderr with loop.add_reader() / loop.add_writer() implicitly changes their blocking mode from blocking to non-blocking. This behaviour does not occur under the standard asyncio selector event loop.

This is causing some issues when running with rich/promptkit in fast-agent

Under the following test code

import anyio
import sys
import os
import asyncio

async def main():

    loop = asyncio.get_running_loop()
    print(f"Using event loop: {type(loop)}")

    stdin = sys.stdin.fileno()
    stdout = sys.stdout.fileno()
    stderr = sys.stderr.fileno()

    print(f"initial:\tstdin fd: {stdin}, stdout fd: {stdout}, stderr fd: {stderr}")
    print(f"blocking:\tstdin fd: {os.get_blocking(stdin)}, stdout fd: {os.get_blocking(stdout)}, stderr fd: {os.get_blocking(stderr)}")

    loop.add_writer(stdin, lambda: None)
    loop.add_writer(stdout, lambda: None)
    loop.add_writer(stderr, lambda: None)

    print(f"writer: \tstdin fd: {stdin}, stdout fd: {stdout}, stderr fd: {stderr}")
    print(f"blocking:\tstdin fd: {os.get_blocking(stdin)}, stdout fd: {os.get_blocking(stdout)}, stderr fd: {os.get_blocking(stderr)}")

    loop.add_reader(stdin, lambda: None)
    loop.add_reader(stdout, lambda: None)
    loop.add_reader(stderr, lambda: None)

    print(f"reader: \tstdin fd: {stdin}, stdout fd: {stdout}, stderr fd: {stderr}")
    print(f"blocking:\tstdin fd: {os.get_blocking(stdin)}, stdout fd: {os.get_blocking(stdout)}, stderr fd: {os.get_blocking(stderr)}")

anyio.run(main, backend="asyncio", backend_options={"use_uvloop": False})
print("-" * 40)
anyio.run(main, backend="asyncio", backend_options={"use_uvloop": True})

I see the following output, where the blocking status of the stdio file descriptors are changed from blocking to non blocking when running under uvloop but not asyncio.

Using event loop: <class 'asyncio.unix_events._UnixSelectorEventLoop'>
initial:        stdin fd: 0, stdout fd: 1, stderr fd: 2
blocking:       stdin fd: True, stdout fd: True, stderr fd: True
writer:         stdin fd: 0, stdout fd: 1, stderr fd: 2
blocking:       stdin fd: True, stdout fd: True, stderr fd: True
reader:         stdin fd: 0, stdout fd: 1, stderr fd: 2
blocking:       stdin fd: True, stdout fd: True, stderr fd: True
----------------------------------------
Using event loop: <class 'uvloop.Loop'>
initial:        stdin fd: 0, stdout fd: 1, stderr fd: 2
blocking:       stdin fd: True, stdout fd: True, stderr fd: True
writer:         stdin fd: 0, stdout fd: 1, stderr fd: 2
blocking:       stdin fd: False, stdout fd: False, stderr fd: False
reader:         stdin fd: 0, stdout fd: 1, stderr fd: 2
blocking:       stdin fd: False, stdout fd: False, stderr fd: False

贡献指南

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

从这里开始

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

调研方向

首先运行提供的 asyncio 与 uvloop 对比复现,并检查 uvloop 中针对 loop.add_reader() 和 loop.add_writer() 的入口点。将它们对文件描述符的处理与标准 asyncio selector loop 进行比较;当在 uvloop 下注册 stdin、stdout 和 stderr 能保留它们的阻塞状态时,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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