Emit warning when Response.background discards injected BackgroundTasks#15112
Open
mango766 wants to merge 2 commits intofastapi:masterfrom
Open
Emit warning when Response.background discards injected BackgroundTasks#15112mango766 wants to merge 2 commits intofastapi:masterfrom
mango766 wants to merge 2 commits intofastapi:masterfrom
Conversation
…BackgroundTasks When an endpoint injects BackgroundTasks via dependency injection AND returns a Response that already has its own `background` attribute set, the injected tasks are silently dropped. This is a confusing footgun that causes tasks to disappear without any indication. This change adds a UserWarning in that scenario so the silent data loss becomes visible. The warning message explains how to resolve the conflict (either use the injected BackgroundTasks exclusively, or stop injecting BackgroundTasks when using Response.background directly). Ref: fastapi#11215 Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UserWarningwhen an endpoint returns aResponsethat already has abackgroundattribute while dependency-injectedBackgroundTasksare also present, since the injected tasks would otherwise be silently discardedCloses #15111
Related: #11215
Details
The fix is intentionally conservative (warning only, no behavior change) as discussed by @YuriiMotov:
The warning message explains to the developer how to resolve the conflict:
BackgroundTasksinstance instead of settingbackgroundon the response, ORBackgroundTaskswhen usingResponse.backgrounddirectly.Test plan
test_warn_when_response_background_overwrites_injected_tasks- verifies UserWarning is emitted when both injected BackgroundTasks and Response.background are presenttest_no_warn_when_response_has_no_background- verifies no warning when response has no background (injected tasks get attached normally)test_no_warn_when_no_injected_background_tasks- verifies no warning when no BackgroundTasks were injectedAll existing tests pass unchanged.