docs: warn about BackgroundTasks + Response background interaction#15217
docs: warn about BackgroundTasks + Response background interaction#15217ChunkyTortoise wants to merge 1 commit intofastapi:masterfrom
Conversation
Add a warning to the "Return a Response Directly" docs page explaining that returning a Response with a background parameter silently overrides any BackgroundTasks added via dependency injection. Closes fastapi#11215
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 487362fd31
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| background_tasks.add_task(write_log, "This task will NOT run") | ||
| return JSONResponse( | ||
| content={"message": "done"}, | ||
| background=None, # If this were a BackgroundTask, it would override the line above |
There was a problem hiding this comment.
Use a non-None
background in the “wrong” example
The /wrong example is labeled as demonstrating that injected BackgroundTasks are dropped, but background=None does not trigger that behavior. FastAPI only skips injected tasks when response.background is non-None, so this snippet still runs background_tasks.add_task(...) and contradicts the warning text. Readers copying this example won’t reproduce the documented pitfall, which makes the new guidance misleading.
Useful? React with 👍 / 👎.
📝 Docs previewLast commit 487362f at: https://f5bf71a2.fastapitiangolo.pages.dev Modified Pages |
YuriiMotov
left a comment
There was a problem hiding this comment.
We are waiting for Sebastian to review this issue and take a decision on how to resolve this:
- Merge BG tasks
- Document + Emit a warning
- Just document
If we decide to go with 3rd approach then we can take this PR and work a bit more on it (add tests (all code examples must be covered by tests), modify text to follow the style of other pages (probably split code example into good and bad, and put explanations as text outside of code examples)).
Let's wait for Sebastian to take a decision
Summary
Closes #11215
Adds a warning to the "Return a Response Directly" docs page explaining that returning a
Responsewith abackgroundparameter silently overrides anyBackgroundTasksadded via dependency injection.Problem
When an endpoint uses both
BackgroundTasksdependency injection and returns aResponse(background=BackgroundTask(...)), the injected tasks are silently dropped. This happens becausefastapi/routing.pyonly assigns injected background tasks whenresponse.background is None:This is a common footgun with no runtime warning -- tasks simply don't execute.
Changes
//github.com/ warningblock todocs/en/docs/advanced/response-directly.mdexplaining the interactiondocs_src/response_directly/tutorial003.pywith correct and incorrect patternsdocs/en/docs/tutorial/background-tasks.mdMotivation
I use FastAPI
BackgroundTasksacross 5 production APIs and nearly hit this exact issue when refactoring webhook delivery to return customResponseobjects. The current docs don't mention this interaction anywhere.Test plan
//github.com/ warningblock syntax (FastAPI convention)🤖 Generated with Claude Code