🐛 Fix ignoring background tasks added after yield#14192
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
44b07c2 to
ce9f04c
Compare
This comment was marked as resolved.
This comment was marked as resolved.
609d48b to
6085f71
Compare
Add test to verify that background tasks added _after_ a `yield` statement are executed correctly. This is a regression introduced in 0.118. Ref: fastapi#14137 Signed-off-by: JP-Ellis <[email protected]>
This fixes a regression introduced in 0.118 whereby background tasks added _after_ a `yield` within a dependency don't get executed. Ref: fastapi#14137 Signed-off-by: JP-Ellis <[email protected]>
6085f71 to
c104ff7
Compare
|
Any update on this PR? |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
yield
This comment was marked as resolved.
This comment was marked as resolved.
…sk-after-yield-regression
|
I pushed some updates:
If you disagree with any of those changes - feel free to revert, I will then open alternative PR and we will ask Sebastian to decide. The main question now is: do we want to allow users to use results of dependencies with yield in background tasks? |
c255d9f to
a3c0abb
Compare
|
Thanks for the update, looks good to me. To answer you question:
No, I don't think so. Starlette does not offer any way to access any results from background tasks, and I don't think FastAPI should deviate from that. Even if you did somehow allow for the results to be accessed, where would that happen? It is executed after the async stack, so it is not available to any of the dependencies, nor the main handler for the endpoint. |
|
Thanks for the interest and work here! ☕ I was checking #14137 and replied there. I think your use case can be solved with the new Dependency scopes: https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-with-yield/#early-exit-and-scope I also need to review and improve all the docs around dependencies. 🤓 On the other hand, I think that there's a chance to improve further the idea of background tasks in a way that could go beyond what Starlette can offer by default, but that's something to explore in the future. I think this specific use case would be solved by the new dependencies with scopes, so I'll pass on this PR, but thanks for the work and conversation everyone here! 🍰 |
Unfortunately, in my particular use case, it is not. I have provided a more complete response to this in the linked discussion. |
This fixes a regression introduced in 0.118 whereby background tasks added after a
yieldwithin a dependency don't get executed.Resolves: #14137