Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adding a middleware causes background tasks cancellation #5606

@csuriano23

Description

@csuriano23

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import asyncio
import time
from fastapi import BackgroundTasks, FastAPI

app = FastAPI()


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


async def test_bg():
    try:
        print("BG TASK STARTED")
        await asyncio.sleep(5)
        print("BG TASK ENDED")
    except BaseException as be:
        print("BG TASK NOT ENDED CORRECTLY", type(be))


@app.get("/hello")
async def root(background_tasks: BackgroundTasks):
    background_tasks.add_task(test_bg)
    return {"message": "hello"}

Description

Executing the sample code causes an unexpected (and apparently unrelated) asyncio CancelledError on long running background tasks.

Sample output running on uvicorn and calling GET on /hello endpoint:

INFO:     Started server process [95599]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:57617 - "GET /hello HTTP/1.1" 200 OK
BG TASK STARTED
BG TASK NOT ENDED CORRECTLY <class 'asyncio.exceptions.CancelledError'>

The cause seems to be the introduction of a middleware; when removing the middleware fragment everything get fine:

INFO:     Started server process [96777]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:57674 - "GET /hello HTTP/1.1" 200 OK
BG TASK STARTED
BG TASK ENDED

Operating System

macOS Ventura

Operating System Details

Darwin ARM64 M1

FastAPI Version

0.86.0

Python Version

Python 3.10.8

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions