1

I’m running into strange behavior in FastAPI where my BackgroundTasks only execute when declared inside an async dependency. No exception is raised, the route returns normally, but the background job never runs.

from fastapi import FastAPI, BackgroundTasks, Depends
app = FastAPI()
async def log_something():
 print("Background task executed!")
async def dependency(bg: BackgroundTasks):
 print("Dependency started")
 bg.add_task(log_something)
 print("Dependency added task")
 return {"ok": True}
@app.get("/test")
async def test_route(data=Depends(dependency)):
 print("Route executed")
 return {"status": "done"}

When I call:

curl http://127.0.0.1:8000/test

I expect to see this printed in the console:

Dependency started
Dependency added task
Route executed
Background task executed!

What I get:

Dependency started
Dependency added task
Route executed

No errors are logged, and FastAPI returns {"status": "done"} correctly. If I move the bg.add_task() call into the route itself, it works as expected.

FastAPI 0.115.2
Python 3.13
macOS (Apple Silicon)
asked Oct 24 at 18:22
New contributor
user31749517 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • BackgroundTasks only runs for tasks that are attached to the response object created by FastAPI during route execution. When you move the bg.add_task(), the bg instance is the same one attached to the response lifecycle, so FastAPI executes the background tasks after returning the response. Commented Oct 24 at 18:29
  • I believe return {"ok": True} doesn't include the BackgroundTasks. You need to attach the BackgroundTasks object to the response from your dependency. FastAPI doesn’t attach that task to the response, because the dependency runs before the route creates the response. That’s why your log_something() never executes, nothing is actually scheduling it after the response is returned. When you call bg.add_task() inside the route itself: FastAPI can attach it to the response being returned. Once the response is sent, FastAPI executes the background task correctly. Commented Oct 24 at 18:31

1 Answer 1

4

BackgroundTasks only runs for tasks that are attached to the response object created by FastAPI during route execution. When you move the bg.add_task(), the bg instance is the same one attached to the response lifecycle, so FastAPI executes the background tasks after returning the response.

Pass BackgroundTasks from the route into your dependency.

async def dependency(bg: BackgroundTasks):
 print("Dependency started")
 bg.add_task(log_something)
 print("Dependency added task")
 return {"ok": True}
@app.get("/test")
async def test_route(bg: BackgroundTasks, data=Depends(dependency)):
 print("Route executed")
 return {"status": "done"}
answered Oct 24 at 18:30
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.