Potential footgun when using custom Response(background=<Task()>)
in conjunction with injected BackgroundTasks
- the custom response overwrites the tasks
#11215
Labels
question
Question or problem
Discussed in #11214
Originally posted by netanel-haber February 28, 2024
First Check
Commit to Help
Example Code
Description
Hey! Thanks for the best framework in Python and generally ❤️
Basically, passing a background task to a custom response overwrites any other tasks added to the injected
BackgroundTasks
.I'm not sure this is considered a bug, but I think it's confusing and can at least be clarified in the Return a Response Directly docs, for example. Other solutions can be actually changing this behavior (I guess that's a breaking change), or printing a warning/throwing [throwing is also a breaking change] if both mechanisms are used at once. But I can't imagine a user desiring this behavior. To be clear, this tripped us up when we couldn't figure out why some of our tasks were being swallowed.
I'd be happy to work on either solution, obviously adding a docs pr would be the easiest first step.
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.97.0
Pydantic Version
1.10.7
Python Version
3.9
Additional Context
No response
The text was updated successfully, but these errors were encountered: