Skip to content

inconsistent file descriptor blocking state when running in asyncio vs uvloop #712

@SecretiveShell

Description

@SecretiveShell

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions