uvloop icon indicating copy to clipboard operation
uvloop copied to clipboard

AttributeError: type object 'async_generator_asend' has no attribute 'f_lineno'

Open theodore86 opened this issue 2 months ago • 1 comments

Bug Description

Getting AttributeError: type object 'async_generator_asend' has no attribute 'f_lineno' exceptions when handling multipart form uploads in FastAPI with uvloop.

Environment

  • Python version: 3.12.11
  • uvloop version: 0.21.0
  • OS: Linux (Docker)
  • Framework: FastAPI with uvicorn

Steps to Reproduce

  1. Use FastAPI with uvloop enabled
  2. Upload file via multipart form data
  3. Exception appears in logs during async generator cleanup

Full Traceback

Exception ignored in: <async_generator object Request.stream at 0x7f6f1be8f920>
Traceback (most recent call last):
  File "uvloop/loop.pyx", line 3187, in uvloop.loop.Loop._asyncgen_finalizer_hook
  File "uvloop/loop.pyx", line 1289, in uvloop.loop.Loop.call_soon_threadsafe
  File "uvloop/cbhandles.pyx", line 329, in uvloop.loop.new_Handle
  File "uvloop/cbhandles.pyx", line 15, in uvloop.loop.Handle._set_loop
  File "uvloop/cbhandles.pyx", line 427, in uvloop.loop.extract_stack
  File "/usr/local/lib/python3.12/traceback.py", line 395, in extract
    return klass._extract_from_extended_frame_gen(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/traceback.py", line 418, in _extract_from_extended_frame_gen
    for f, (lineno, end_lineno, colno, end_colno) in frame_gen:
                                                     ^^^^^^^^^
  File "/usr/local/lib/python3.12/traceback.py", line 392, in extended_frame_gen
    for f, lineno in frame_gen:
                     ^^^^^^^^^
  File "/usr/local/lib/python3.12/traceback.py", line 336, in walk_stack
    yield f, f.f_lineno
             ^^^^^^^^^^
AttributeError: type object 'async_generator_asend' has no attribute 'f_lineno'

theodore86 avatar Oct 15 '25 22:10 theodore86

same question.

  • reproduce code
# main.py
import uvicorn
from fastapi import FastAPI, Request
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse

app = FastAPI()

# Define three simple middlewares. The third one will try to read the request body.
class FirstMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        print("Entering First Middleware")
        response = await call_next(request)
        print("Exiting First Middleware")
        return response

class SecondMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        print("Entering Second Middleware")
        response = await call_next(request)
        print("Exiting Second Middleware")
        return response

class ThirdMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        response = await call_next(request)
        return response


# Add the three middlewares to the application
app.add_middleware(FirstMiddleware)
app.add_middleware(SecondMiddleware)
app.add_middleware(ThirdMiddleware) # <-- If you comment out this line, or any of the others, the error disappears.

@app.post("/test")
async def test_endpoint(request: Request):
    # The endpoint can also try to access the body, which still works
    # because BaseHTTPMiddleware caches it.
    body = await request.json()
    return JSONResponse({"message": "success", "body_received": body})

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8090)
  • .env
PYTHONASYNCIODEBUG=1

Error

Exception ignored in: <async_generator object Request.stream at 0x106c97680>
Traceback (most recent call last):
  File "uvloop/loop.pyx", line 3187, in uvloop.loop.Loop._asyncgen_finalizer_hook
  File "uvloop/loop.pyx", line 1289, in uvloop.loop.Loop.call_soon_threadsafe
  File "uvloop/cbhandles.pyx", line 329, in uvloop.loop.new_Handle
  File "uvloop/cbhandles.pyx", line 15, in uvloop.loop.Handle._set_loop
  File "uvloop/cbhandles.pyx", line 427, in uvloop.loop.extract_stack
  File "/Users/admin/miniconda3/envs/py312/lib/python3.12/traceback.py", line 395, in extract
    return klass._extract_from_extended_frame_gen(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/admin/miniconda3/envs/py312/lib/python3.12/traceback.py", line 418, in _extract_from_extended_frame_gen
    for f, (lineno, end_lineno, colno, end_colno) in frame_gen:
                                                     ^^^^^^^^^
  File "/Users/admin/miniconda3/envs/py312/lib/python3.12/traceback.py", line 392, in extended_frame_gen
    for f, lineno in frame_gen:
                     ^^^^^^^^^
  File "/Users/admin/miniconda3/envs/py312/lib/python3.12/traceback.py", line 336, in walk_stack
    yield f, f.f_lineno
             ^^^^^^^^^^
AttributeError: type object 'StopIteration' has no attribute 'f_lineno'

gloryfromca avatar Nov 04 '25 05:11 gloryfromca