Repository navigation
Return a 500 response from WSGITransport when raise_app_exceptions=False - #3882
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
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
marked this pull request as ready for review
November 29, 2026 01:33
atishay-kasliwal
left a comment
There was a problem hiding this comment.
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.
| # so that `raise_app_exceptions=False` returns an error response | ||
| # rather than propagating the exception. | ||
| result = [] | ||
| if seen_status is None: |
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WSGITransport(raise_app_exceptions=False)only suppresses exceptions that arrive throughstart_response(..., exc_info). When the application raises directly, the exception still propagates out of the client:ASGITransportreturns a500for the same application — #2669 added that, along withtests/test_asgi.py::test_asgi_exc_no_raise— so the two transports disagree on the same switch, anddocs/advanced/transports.mddocuments it identically for both ("Inspect 500 error responses ratherthan 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 a500status, keepingthe status already passed to
start_response()if there was one (so an application that fails afterstarting a response does not have its status replaced).
Checklist
Discussed in WSGITransport ignores raise_app_exceptions=False when the app raises #3881
single atomic change.
*
test_wsgi_exc_no_raise— the app raises beforestart_response():500, empty body.*
test_wsgi_exc_no_raise_after_response— the app raises afterstart_response("200 OK"): the200is kept.*
test_wsgi_exc_raise— the defaultraise_app_exceptions=Truestill propagates.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, coverage100%.scripts/checkclean (ruff 0.12.11, mypy 1.17.1).