From 06b7bae7c067e1c19fefa2c1a9fe7dcf4b42b8ae Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 12 Apr 2026 18:04:10 -0400 Subject: [PATCH] Fix blocking file I/O in Response.stream_file() The async generator was using synchronous open() and read() which blocks the event loop. Switched to anyio.open_file() for proper async file I/O. Co-Authored-By: Claude Opus 4.6 (1M context) --- responder/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/responder/models.py b/responder/models.py index d12ba80..9c4ba09 100644 --- a/responder/models.py +++ b/responder/models.py @@ -464,9 +464,11 @@ class Response: self.mimetype = guessed or "application/octet-stream" async def file_generator(): - with open(path, "rb") as f: + import anyio + + async with await anyio.open_file(path, "rb") as f: while True: - chunk = f.read(chunk_size) + chunk = await f.read(chunk_size) if not chunk: break yield chunk