<spam>#6008
Closed
shr7q wants to merge 1 commit intopallets:mainfrom
Closed
Conversation
Root cause: In `src/flask/app.py`, the `process_response` method calls each `after_request` function and reassigns the response to its return value without checking whether the return value is `None`. When a hook Resolved by BugSlayer
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Improve error messages when after_request hooks forget to return response
Summary
This PR adds validation in the
process_responsemethod to detect whenafter_requesthooks returnNoneand raise an immediate, descriptive error. Previously, returningNonefrom anafter_requesthook would silently set the response toNone, causing a crypticTypeError: 'NoneType' object is not callablelater during WSGI response delivery. Now users get a clear message identifying the offending hook by name.Root Cause
The
process_responsemethod insrc/flask/app.pycalls eachafter_requestfunction and reassigns the response variable to its return value without validation. When a hook returnsNone(e.g.,lambda _: None), the response becomesNone. This error only surfaces later inwsgi_appwhen Flask attempts to callresponse(environ, start_response), producing an unhelpful error message that doesn't indicate anafter_requesthook is responsible.Changes Made
Modified the
process_responsemethod insrc/flask/app.pyto validate the return value of eachafter_requestfunction. After calling each hook, we now check if the return value isNoneand raise aTypeErrorwith a descriptive message that includes the hook's function name. This validation applies to allafter_requestfunctions across both the app and blueprint scopes.Testing
All 487 existing tests pass with this change. The fix provides immediate feedback at the point of failure, making debugging significantly easier for users who accidentally omit a return statement in their
after_requesthooks.