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 摘要。