Skip to content

Emit warning when Response.background discards injected BackgroundTasks#15112

Open
mango766 wants to merge 2 commits intofastapi:masterfrom
mango766:fix/warn-background-tasks-overwritten
Open

Emit warning when Response.background discards injected BackgroundTasks#15112
mango766 wants to merge 2 commits intofastapi:masterfrom
mango766:fix/warn-background-tasks-overwritten

Conversation

@mango766
Copy link
Copy Markdown

Summary

  • Adds a UserWarning when an endpoint returns a Response that already has a background attribute while dependency-injected BackgroundTasks are also present, since the injected tasks would otherwise be silently discarded
  • Adds test coverage for the warning behavior (emitted when both are present) and the no-warning cases (only injected tasks, only response background)

Closes #15111
Related: #11215

Details

The fix is intentionally conservative (warning only, no behavior change) as discussed by @YuriiMotov:

The solution might be to add a warning here in such cases... This way it will not happen silently and user can easily fix this.

The warning message explains to the developer how to resolve the conflict:

  1. Add tasks to the injected BackgroundTasks instance instead of setting background on the response, OR
  2. Stop injecting BackgroundTasks when using Response.background directly.

Test plan

  • test_warn_when_response_background_overwrites_injected_tasks - verifies UserWarning is emitted when both injected BackgroundTasks and Response.background are present
  • test_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 injected

All existing tests pass unchanged.

easonysliu and others added 2 commits March 14, 2026 14:08
…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>
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 14, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing mango766:fix/warn-background-tasks-overwritten (cb0c5f8) with master (4b82602)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (04b279f) during the generation of this report, so 4b82602 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Injected BackgroundTasks silently discarded when Response has its own background

2 participants