Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BackgroundTasks with BaseHTTPMiddleware #2688

Merged
merged 4 commits into from
Sep 7, 2024
Merged

Fix BackgroundTasks with BaseHTTPMiddleware #2688

merged 4 commits into from
Sep 7, 2024

Conversation

adriangb
Copy link
Member

@adriangb adriangb commented Sep 6, 2024

Alternative to #2176

@dmitry-mli
Copy link

FYI, The streaming response used to also do:

            if not isinstance(chunk, (bytes, memoryview)):
                chunk = chunk.encode(self.charset)

It itsn't covered so when it was removed in https://github.com/encode/starlette/pull/2620/files it didn't break

@@ -222,3 +222,6 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
await send({"type": "http.response.body", "body": chunk, "more_body": True})

await send({"type": "http.response.body", "body": b"", "more_body": False})

if self.background:
await self.background()
Copy link

@dmitry-mli dmitry-mli Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For full backward compatibility add it to constructor also:

class _StreamingResponse(Response):
    def __init__(
        self,
        content: AsyncContentStream,
        status_code: int = 200,
        headers: typing.Mapping[str, str] | None = None,
        media_type: str | None = None,
        background: BackgroundTask | None = None,                <<<
        info: typing.Mapping[str, typing.Any] | None = None,
    ) -> None:
        self.info = info
        self.body_iterator = content
        self.status_code = status_code
        self.media_type = media_type
        self.background = background                             <<<
        self.init_headers(headers)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because some middleware may be referring to that field. Or below will work also

self.background = None

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No users should be calling the constructor unless they do something like type(response)(...) which is not something we need to support.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, this aligns with the class being marked as private. Then please include

self.background = None

For full backward compatibility, any response object injected into BaseHTTPMiddleware based middleware needs to have the background field (default None)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see your point now with the edit. Good point. Added.

@Kludex this is another reason to move the background task logic out of responses and a good reason to rework our Response inheritance so that Response and StreamingResponse both inherit from a BaseResponse that has the API and initialization of common bits.

@adriangb adriangb enabled auto-merge (squash) September 6, 2024 23:07
@Kludex Kludex disabled auto-merge September 7, 2024 04:41
Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test here?

@adriangb
Copy link
Member Author

adriangb commented Sep 7, 2024

There is a test already, am I missing something?

@Kludex Kludex merged commit 53f9dc0 into master Sep 7, 2024
6 checks passed
@Kludex Kludex deleted the simpler branch September 7, 2024 12:42
nixroxursox pushed a commit to nixroxursox/starlette that referenced this pull request Sep 30, 2024
* Streaming response early disconnect mode

* Fix BackgroundTasks with BaseHTTPMiddleware

* move comment

* initialize field

---------

Co-authored-by: Dmitry Maliuga <dmaliuga@fireworks.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants