Skip to content

AI junk#6100

Closed
ghost wants to merge 1 commit into
mainfrom
unknown repository
Closed

AI junk#6100
ghost wants to merge 1 commit into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Jul 20, 2026

Copy link
Copy Markdown

In wsgi_app, the call to response(environ, start_response) sits outside the inner try/except that catches dispatch errors. When this call raises (for example, a streaming response body fails or the client disconnects mid-delivery), the error variable remains None. The finally block then calls ctx.pop(error) with error=None, so all teardown_request functions receive exc=None instead of the actual exception. This silently discards the error, making it impossible for teardown logic to make informed cleanup decisions (such as rolling back a database transaction versus committing).

The fix wraps response(environ, start_response) in a try/except BaseException that captures the error into the error variable before re-raising. The finally block then passes the correct exception to ctx.pop(error). Using BaseException ensures that KeyboardInterrupt, SystemExit, and other non-Exception throwables during WSGI response delivery are also captured. The exception is always re-raised after capture — it is never swallowed or handled, preserving the existing contract that wsgi_app propagates unexpected errors to the WSGI server.

A regression test is added that creates a Response subclass whose __call__ raises RuntimeError, registers a teardown_request handler, and asserts that the handler receives the RuntimeError instance. This test fails on the unpatched code (teardown receives None) and passes with the fix. The full Flask test suite (492 tests) passes with no regressions.

@davidism davidism closed this Jul 20, 2026
@davidism davidism changed the title Capture WSGI response delivery errors for teardown functions in wsgi_app AI junk Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant