Skip to content
Closed

AI junk #6100

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: 5 additions & 1 deletion src/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,11 @@ def wsgi_app(
except:
error = sys.exc_info()[1]
raise
return response(environ, start_response)
try:
return response(environ, start_response)
except BaseException as e:
error = e
raise
finally:
if "werkzeug.debug.preserve_context" in environ:
environ["werkzeug.debug.preserve_context"](ctx)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,3 +1968,29 @@ def test_app_freed_on_zero_refcount():
assert weak() is None
finally:
gc.enable()


def test_teardown_receives_wsgi_response_error(app, client):
"""Teardown functions receive the exception when
response(environ, start_response) raises during WSGI response
delivery, not exc=None."""

@app.teardown_request
def teardown(exc):
app.config["_teardown_exc"] = exc

class FailingResponse(flask.Response):
def __call__(self, environ, start_response):
raise RuntimeError("response delivery failed")

@app.route("/")
def index():
return FailingResponse("ok")

try:
client.get("/")
except RuntimeError:
pass

assert app.config.get("_teardown_exc") is not None
assert isinstance(app.config["_teardown_exc"], RuntimeError)
Loading