fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

middleware breaks behavior of background task

Open jkh911208 opened this issue 6 years ago • 10 comments

First check

  • [x ] I added a very descriptive title to this issue.
  • [ x] I used the GitHub search to find a similar issue and didn't find it.
  • [ x] I searched the FastAPI documentation, with the integrated search.
  • [ x] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [ x] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [ x] I already checked if it is not related to FastAPI but to Pydantic.
  • [ x] I already checked if it is not related to FastAPI but to Swagger UI.
  • [ x] I already checked if it is not related to FastAPI but to ReDoc.
  • [ x] After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

import asyncio
import logging
import time

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from starlette.background import BackgroundTasks

app = FastAPI()


@app.get("/")
async def read_root():
    response_object = {"Hello": "World"}
    tasks = BackgroundTasks()
    tasks.add_task(bg_task)
    return JSONResponse(response_object, 202, background=tasks)


async def bg_task():
    logging.error("test1")
    await asyncio.sleep(10)
    logging.error("test2")


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    response.headers["X-Process-Time"] = str(time.time() - start_time)
    response.headers["address"] = request.client.host
    return response

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="0.0.0.0", port=8000,
                log_level="debug", reload=True)

Description

run the above code, make a request to "/" you will see Screen Shot 2020-10-21 at 8 50 42 AM which logging is happening before return the object

but if you make only one request at a time this is fine, but if you make multiple request while background task is running

problem #1 the second request start after first background task finishes problem #2 when background task of second request finishes, it throw error Screen Shot 2020-10-21 at 8 52 39 AM

Environment

  • OS: [e.g. Linux / Windows / macOS]: macos and debian
  • FastAPI Version [e.g. 0.3.0]: 0.61.1
  • Python version: 3.8.5

Additional context

if i remove middleware code it works as expected Screen Shot 2020-10-21 at 8 55 24 AM all requests are handled and all background tasks are working fine no exception

jkh911208 avatar Oct 21 '20 13:10 jkh911208

I believe this is related to Starlette Issue 919: Background tasks don't work with middleware that subclasses BaseHTTPMiddleware

ycd avatar Oct 21 '20 14:10 ycd

thank you @ycd seems like work is still in progress

jkh911208 avatar Oct 21 '20 14:10 jkh911208

@jkh911208 Could you close this issue, please?

Kludex avatar Oct 25 '20 11:10 Kludex

For those who are searching for how to call background tasks under FastAPI middleware, here is quick solution:

from starlette.background import BackgroundTask from somewhere import functionA

@app.middleware("http") async def middleware(request: Request, call_next): ........(do something) response.background = BackgroundTask(functionA, arg) return response

You can add a time.sleep(10) in function A to see the result better. For tasks running in background, it will return the results of function A very quickly and will not be affected by time.sleep().

Hope this helps someone!

yingyingPengSAP avatar Sep 30 '21 15:09 yingyingPengSAP

Hi @jkh911208 @Kludex , why is this issue Closed? Cause I'm trying to use background tasks and a middleware at the same time, and if I send a request to the endpoint through Postman, I receive my response but the request time doesn't finish until the background task does. But if I remove the middleware then it works as expected.

santigandolfo avatar Apr 18 '22 19:04 santigandolfo

What is the version of your FastAPI?

Oh sorry, yeah, this is still a thing. https://github.com/encode/starlette/pull/1441

Kludex avatar Apr 18 '22 19:04 Kludex

What is the version of your FastAPI?

Oh sorry, yeah, this is still a thing. encode/starlette#1441

I'm on v0.75.2 Do you suggest a workaround until that PR is merged in Starlette and FastAPI bumps it's requirements?

santigandolfo avatar Apr 18 '22 19:04 santigandolfo

Yes, don't use BaseHTTPMiddleware.

Kludex avatar Apr 18 '22 19:04 Kludex

Yes, don't use BaseHTTPMiddleware.

In that case I couldn't add general before and after request actions, right?

santigandolfo avatar Apr 18 '22 20:04 santigandolfo

No, I'm saying to not use that class. You can have middlewares: https://pgjones.dev/blog/how-to-write-asgi-middleware-2021

Kludex avatar Apr 19 '22 03:04 Kludex