Repository navigation
fix: make the request container usable from every request hook - #42
Merged
Merged
Conversation
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.
Closes #41.
The request container no longer depends on hook registration order.
Changes
injectbuilds the request container on first use inside a request.before_requeststill builds it too, through the same get-or-create path, so it never replaces a container an earlier hook already built.gis tied to theflask.Requestit was built for. A second request in the same app context gets its own container. A later lookup in the same request reuses the existing one, even if it is closed, so nothing is rebuilt after the close.injectuses whatever container is ongand raises the sameRuntimeErroras before when there is none. An app context without a request (CLI,app.app_context()) never gets one built.setup_dimoves the close to index 0 ofapp.teardown_appcontext_funcs.Flask.do_teardown_appcontextruns that list in reverse, so the close runs after every other handler, whether it was registered before or aftersetup_di. Callingsetup_diagain keeps one close at the front.g. It never builds one.Why keep the
before_requestcreationOn Flask 3.0 and 3.1,
RequestContext.popresets the request before it pops the app context, soteardown_appcontexthandlers run without a request and can't build a container. If creation were lazy only, ateardown_appcontexthandler that injects would fail on any request where nothing used DI earlier. Building eagerly inbefore_requestcovers that case, and the lazy path covers hooks that run before it: earlybefore_requesthooks, andteardown_requestafter abefore_requesthook short-circuits.Flask API used
app.teardown_appcontext_funcsis a documented attribute onFlask. The rest isflask.has_request_context,flask.current_app,flask.gand the existingflask.request._get_current_object().Streaming
On Flask 3.0,
stream_with_contextkeeps the context open for the whole stream, so the generator uses the live container. Since Flask 3.1.2 the context is torn down before the body is iterated and pushed again, so the generator sees the closed container and getsContainerClosedError. That already happened before this change. The test accepts either outcome and checks that no second container is built.