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(compression middleware): Fix resource handling #3927

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions litestar/middleware/compression/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@
if initial_message is not None and value_or_default(connection_state.is_cached, False):
await send(initial_message)
await send(message)
facade.close()
return

if initial_message and message["type"] == "http.disconnect":
facade.close()

Check warning on line 138 in litestar/middleware/compression/middleware.py

View check run for this annotation

Codecov / codecov/patch

litestar/middleware/compression/middleware.py#L138

Added line #L138 was not covered by tests
return

if initial_message and message["type"] == "http.response.body":
Expand Down Expand Up @@ -170,6 +175,7 @@
await send(message)

else:
facade.close()
await send(initial_message)
await send(message)

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_middleware/test_compression_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ async def fake_send(message: Message) -> None:
# second body message with more_body=True will be empty if zlib buffers output and is not flushed
await wrapped_send(HTTPResponseBodyEvent(type="http.response.body", body=b"abc", more_body=True))
assert mock.mock_calls[-1].args[0]["body"]
# send a more_body=False so resources close properly
await wrapped_send(HTTPResponseBodyEvent(type="http.response.body", body=b"", more_body=False))


@pytest.mark.parametrize(
Expand Down
Loading