Skip to content

Commit

Permalink
fix(compression middleware): Fix resource handling (litestar-org#3927)
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut authored Jan 5, 2025
1 parent f5ba3a7 commit 2fb6f6b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
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 @@ async def send_wrapper(message: Message) -> None:
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()
return

if initial_message and message["type"] == "http.response.body":
Expand Down Expand Up @@ -170,6 +175,7 @@ async def send_wrapper(message: Message) -> None:
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

0 comments on commit 2fb6f6b

Please sign in to comment.