Thanks to visit codestin.com
Credit goes to github.com

Skip to content

🐛 Fix ignoring background tasks added after yield#14192

Closed
JP-Ellis wants to merge 5 commits into
fastapi:masterfrom
JP-Ellis:fix/background-task-after-yield-regression
Closed

🐛 Fix ignoring background tasks added after yield#14192
JP-Ellis wants to merge 5 commits into
fastapi:masterfrom
JP-Ellis:fix/background-task-after-yield-regression

Conversation

@JP-Ellis

Copy link
Copy Markdown

This fixes a regression introduced in 0.118 whereby background tasks added after a yield within a dependency don't get executed.

Resolves: #14137

@JP-Ellis

This comment was marked as resolved.

@svlandeg svlandeg added the bug Something isn't working label Oct 20, 2025
@JP-Ellis JP-Ellis changed the title chore: add background dependency regression test fix: background task ignored after yield Oct 21, 2025
@JP-Ellis JP-Ellis force-pushed the fix/background-task-after-yield-regression branch from 44b07c2 to ce9f04c Compare October 21, 2025 00:06
@JP-Ellis

This comment was marked as resolved.

@JP-Ellis JP-Ellis force-pushed the fix/background-task-after-yield-regression branch 3 times, most recently from 609d48b to 6085f71 Compare October 21, 2025 00:41
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]>
@JP-Ellis JP-Ellis force-pushed the fix/background-task-after-yield-regression branch from 6085f71 to c104ff7 Compare October 21, 2025 00:46
@ezeparziale

Copy link
Copy Markdown

Any update on this PR?

YuriiMotov

This comment was marked as resolved.

@JP-Ellis

This comment was marked as resolved.

@github-actions github-actions Bot removed the waiting label Oct 28, 2025
@YuriiMotov

This comment was marked as resolved.

@JP-Ellis

This comment was marked as resolved.

@JP-Ellis

This comment was marked as resolved.

@github-actions github-actions Bot removed the waiting label Nov 2, 2025
@YuriiMotov

This comment was marked as resolved.

@YuriiMotov YuriiMotov changed the title fix: background task ignored after yield 🐛 Fix ignoring background tasks added after yield Nov 3, 2025
@github-actions github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Nov 3, 2025
@github-actions

This comment was marked as resolved.

@github-actions github-actions Bot removed the conflicts Automatically generated when a PR has a merge conflict label Nov 7, 2025
@YuriiMotov

YuriiMotov commented Nov 7, 2025

Copy link
Copy Markdown
Member

I pushed some updates:

  • merged master into PR's branch
  • updated code to execute all bg tasks after sending response and exiting async stack
  • updated tests - I think it's enough to test only async endpoint + async dependency. Also, I added asserts to ensure bg tasks executed only once and dependency is finalized at the moment of bg tasks execution

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?
According to the docs, results of dependencies with yield are not supposed to be used in background tasks. If it's not a mistake in docs, then we are on the right way

@YuriiMotov YuriiMotov force-pushed the fix/background-task-after-yield-regression branch from c255d9f to a3c0abb Compare November 7, 2025 21:12
@JP-Ellis

JP-Ellis commented Nov 8, 2025

Copy link
Copy Markdown
Author

Thanks for the update, looks good to me.

To answer you question:

Do we want to allow users to use results of dependencies with yield in background tasks?

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.

@tiangolo

Copy link
Copy Markdown
Member

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! 🍰

@tiangolo tiangolo closed this Nov 20, 2025
@JP-Ellis

Copy link
Copy Markdown
Author

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.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants