Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a middleware causes background tasks cancellation #5606

Closed
9 tasks done
csuriano23 opened this issue Nov 9, 2022 · 4 comments
Closed
9 tasks done

Adding a middleware causes background tasks cancellation #5606

csuriano23 opened this issue Nov 9, 2022 · 4 comments

Comments

@csuriano23
Copy link

csuriano23 commented Nov 9, 2022

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

@csuriano23 csuriano23 added the question Question or problem label Nov 9, 2022
@odiseo0
Copy link

odiseo0 commented Nov 10, 2022

I believe this is an issue with BaseHTTPMiddleware from Starlette

See BaseHTTPMiddleware and this issue

Possible solution:
Use a pure ASGI middleware

@csuriano23
Copy link
Author

Thank you. It seems that the issue has been fixed in starlette 0.21, so I'm looking forward to have the release with this #5471

@csuriano23
Copy link
Author

I think this can be closed after latest FastAPI release. Thank you

@tiangolo
Copy link
Owner

Thanks for the help here @odiseo0 ! 👏 🙇

Thanks for reporting back and closing the issue @csuriano23 👍

@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6177 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants