[API] Fix Background Tasks Being Cancelled Prematurely #2618
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.
In the current fastapi version, there is a bug in the starlette package it uses for the background tasks. The bug causes the task to be cancelled if the client's http connection is closed before the task is done. The bug is fixed in the latest version of starlette & fastapi. We will upgrade in 1.3.0, but until then we will use this workaround to prevent the task from being cancelled all together. See encode/starlette#1438 and tiangolo/fastapi#5606