Skip to content

Return a 500 response from WSGITransport when raise_app_exceptions=False - #3882

Open
qianeric-backup wants to merge 1 commit into
encode:masterfrom
qianeric-backup:wsgi-raise-app-exceptions-false
Open

qianeric-backup wants to merge 1 commit into
encode:masterfrom
qianeric-backup:wsgi-raise-app-exceptions-false

Conversation

@qianeric-backup

Copy link
Copy Markdown

Summary

WSGITransport(raise_app_exceptions=False) only suppresses exceptions that arrive through start_response(..., exc_info). When the application raises directly, the exception still propagates out of the client:

def app(environ, start_response):
    raise RuntimeError("boom")

transport = httpx.WSGITransport(app=app, raise_app_exceptions=False)
httpx.Client(transport=transport).get("http://www.example.org/")
# RuntimeError: boom

ASGITransport returns a 500 for the same application — #2669 added that, along with
tests/test_asgi.py::test_asgi_exc_no_raise — so the two transports disagree on the same switch, and
docs/advanced/transports.md documents it identically for both ("Inspect 500 error responses rather
than raise exceptions by setting raise_app_exceptions=False").

The fix mirrors the ASGI handling: wrap the application call, re-raise when
raise_app_exceptions=True, and otherwise fall back to an empty body with a 500 status, keeping
the status already passed to start_response() if there was one (so an application that fails after
starting a response does not have its status replaced).

Checklist

  • I understand that this PR may be closed in case there was no previous discussion.
    Discussed in WSGITransport ignores raise_app_exceptions=False when the app raises #3881
  • I've added a test for each change that was introduced, and I tried as much as possible to make a
    single atomic change.
    * test_wsgi_exc_no_raise — the app raises before start_response(): 500, empty body.
    * test_wsgi_exc_no_raise_after_response — the app raises after start_response("200 OK"): the 200 is kept.
    * test_wsgi_exc_raise — the default raise_app_exceptions=True still propagates.
  • I've updated the documentation accordingly.
    No documentation change is needed: the existing text already describes the behaviour this PR
    makes true for the WSGI transport, matching the ASGI one.

Full suite: 1420 passed, 1 skipped, coverage 100%. scripts/check clean (ruff 0.12.11, mypy 1.17.1).

handle_request only guards the start_response(..., exc_info) path, so an
application that raises directly still propagates its exception even when the
transport was created with raise_app_exceptions=False. ASGITransport returns a
500 for the same application since encode#2669.

Mirror the ASGI handling: wrap the application call, re-raise when
raise_app_exceptions is true, and otherwise fall back to an empty body with a
500 status while keeping any status already passed to start_response().
@qianeric-backup
qianeric-backup marked this pull request as ready for review November 29, 2026 01:33

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ran WSGITransport(raise_app_exceptions=False) with an app that raises before start_response(). It now returns a 500 (master: the RuntimeError propagated). With raise_app_exceptions=True the error still propagates, and normal apps are unchanged.

Comment thread httpx/_transports/wsgi.py
# so that `raise_app_exceptions=False` returns an error response
# rather than propagating the exception.
result = []
if seen_status is None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the app calls start_response('200 OK', ...) and then raises, this keeps the 200 and returns an empty body. The client sees a successful response from an app that crashed. wsgiref returns 500 Internal Server Error for the same app, since WSGI headers aren't sent until the first body chunk. Should the 500 also replace a status that was set but never sent?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants