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 exception suppression in asgi transport #2669

Merged
merged 11 commits into from
May 21, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed

* Return `500` error response instead of exceptions when `raise_app_exceptions=False` is set on `ASGITransport`. (#2669)
tomchristie marked this conversation as resolved.
Show resolved Hide resolved
* Fix optional percent-encoding behaviour. (#2671)
* More robust checking for opening upload files in binary mode. (#2630)
* Properly support IP addresses in `NO_PROXY` environment variable. (#2659)
Expand Down
8 changes: 7 additions & 1 deletion httpx/_transports/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,15 @@ async def send(message: typing.Dict[str, typing.Any]) -> None:
try:
await self.app(scope, receive, send)
except Exception: # noqa: PIE-786
if self.raise_app_exceptions or not response_complete.is_set():
if self.raise_app_exceptions:
raise

response_complete.set()
if status_code is None:
status_code = 500
if response_headers is None:
response_headers = {}

assert response_complete.is_set()
assert status_code is not None
assert response_headers is not None
Expand Down
10 changes: 10 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import httpx
from httpx import ASGITransport


async def hello_world(scope, receive, send):
Expand Down Expand Up @@ -191,3 +192,12 @@ async def read_body(scope, receive, send):

assert response.status_code == 200
assert disconnect


@pytest.mark.anyio
async def test_asgi_exc_no_raise():
transport = ASGITransport(app=raise_exc, raise_app_exceptions=False)
async with httpx.AsyncClient(transport=transport) as client:
response = await client.get("http://www.example.org/")

assert response.status_code == 500
Copy link
Member

Choose a reason for hiding this comment

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

What's the behaviour of this test before the code change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test failed with RuntimeError being raised from raise_exc