abersheeran / a2wsgi

Convert WSGI app to ASGI app or ASGI app to WSGI app.
Apache License 2.0
218 stars 20 forks source link

Task was destroyed but it is pending error #61

Open jeffthompsonsd opened 3 weeks ago

jeffthompsonsd commented 3 weeks ago

Hello, using the following converted fastapi app, serving the resulting wsgi app with gunicorn and then hitting the route with the vegeta load testing tool im seeing the error after about a minute of requests.

import logging

from fastapi import FastAPI, APIRouter
from a2wsgi import ASGIMiddleware

logging.basicConfig(
    filename="app.log",
    level=logging.INFO,
    format="%(asctime)s | [%(thread)d](%(process)d) >>> %(message)s",
    datefmt="%H:%M:%S",
)
logger = logging.getLogger(__name__)

hello = APIRouter()

@hello.get("/hello")
def get_hello():
    return "world"

def create_app():
    app = FastAPI()
    app.include_router(hello)
    return app

def create_wsgi_app():
    return ASGIMiddleware(create_app())

Error starts dumping in the app.log about a minute into the requests

Task was destroyed but it is pending!
task: <Task pending name='Task-4' coro=<FastAPI.__call__() running at ../venv/lib64/python3.11/site-packages/fastapi/applications.py:1055> wait_for=<Future pending cb=[Task.task_wakeup()]> cb=[ASGIResponder.asgi_done_callback()]>

Launching the wsgi app with 10 gunicorn workers

.venv/bin/gunicorn \
        --bind 0.0.0.0:8000 \
        --access-logformat '%(t)s | (%(p)s) %(s)s >>> %(r)s' \
        --log-level 'debug' \
        --error-logfile 'error.log' \
        --access-logfile 'access.log' \
        --max-requests 3 \
        --max-requests-jitter 2 \
        --workers 10 \
        'app:create_wsgi_app()' \
        > gunicorn.log 2> gunicorn_error.log

Hitting the route with the vegeta testing tool

echo "GET http://localhost:8000/hello" | vegeta attack --duration=120s --rate=50 | tee results.bin | vegeta report

The error doesnt happen with an asgi app (uvicorn alone or gunicorn as a process manager with uvicorn workers). Any help is appreciated.

abersheeran commented 3 weeks ago

This seems to be a race condition in Task state. Seems to be related to https://github.com/abersheeran/a2wsgi/issues/53

jeffthompsonsd commented 3 weeks ago

It did seem related to that issue, any ideas? We are seeing it outside of tests.