fastapistarlette

FastAPI middleware masks exceptions in background tasks -- why?


Consider the following minimal example. This is a FastAPI handler that enqueues a background task. This task will throw.

from fastapi import BackgroundTasks, FastAPI, Request, Response, status

app = FastAPI(title="Debug demo", description="", version="0.1.0")

@app.middleware("http")
async def noop_middleware(request: Request, call_next):
    return await call_next(request)

@app.post("/test")
async def test(req: Request, background_tasks: BackgroundTasks) -> Response:
    def _fail():
        raise ValueError("TEST")

    background_tasks.add_task(_fail)

    return Response(content="", status_code=status.HTTP_200_OK)

The app is run via uvicorn (uvicorn my_demo:app --host 0.0.0.0 --port 8000).

If the no-op middleware is commented out, the exception thrown by the task will appear in uvicorn logs:

ERROR:    Exception in ASGI application
Traceback (most recent call last): <...>

This is the desired/expected behavior. However, with the middleware in place, the exceptions will be silently swallowed (not logged). Obviously one can work around this by making sure every background task catches and logs its errors, but the default behavior is surprising. What is going on and how can one avoid middleware from interfering with exception logging?


Solution

  • I think it's a bug of Starlette. I created a simple example code that works as you expect on Starlette 0.28.0, but doesn't log exception on Starlette 0.29.0.

    from starlette.applications import Starlette
    from starlette.middleware import Middleware
    from starlette.middleware.base import BaseHTTPMiddleware
    from starlette.routing import Route
    from starlette.background import BackgroundTask
    from starlette.responses import JSONResponse
    
    class CustomHeaderMiddleware(BaseHTTPMiddleware):
        async def dispatch(self, request, call_next):
            response = await call_next(request)
            return response
    
    async def bg_task():
        raise ValueError("TEST")
    
    def homepage(request):
        task = BackgroundTask(bg_task)
        message = {'status': 'Ok'}
        return JSONResponse(message, background=task)
    
    routes = [
        Route('/', homepage),
    ]
    
    middleware = [
        Middleware(CustomHeaderMiddleware)
    ]
    
    app = Starlette(routes=routes, middleware=middleware)
    

    I think you should ask this question on https://github.com/encode/starlette.

    UPD: There is already an issue there (https://github.com/encode/starlette/issues/2625)