Streaming response early disconnect mode #2687
Closed
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.
Summary
Starlette 0.38.4 introduced a bugfix that also introduced a backward incompatible change. This PR keep both the original fix as well as recovers the backward compatibility through parameterization.
#2620 solved issue
#2620 delivered an important fix for
BaseHTTPMiddleware
which relied onStreamingResponse
for chained upstream and downstream communication. Unfortunately,StreamingResponse
optimized for early disconnect which resulted into unexpected termination for some of middleware handlers, in case the communication did not manage to propagate through a longer stack (a stack of 4 was enough to reproduce).#2620 created issue
Change #2620 introduced a backward incompatible change for streaming response from
BaseHTTPMiddleware
(background tasks were dropped). This impacted software that relied async post-processing.This PR content
This PR extends
StreamingResponse
withearly_disconnect
flag (defaultTrue
) that controls the early disconnect behavior that is undesirable for the middleware caseBaseHTTPMiddleware
. The flag is backward compatible for clients ofStreamingResponse
. The_StreamingResponse
is reverted back to inherit fromStreamingResponse
but this time suppressing the early disconnect mode. Unit tests extended to cover the case.Checklist