New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot inject into teardown_request handler while using request scope #42
Comments
Thanks for the report, this is something I think should be supported. I understand your hesitation, but I don't see any way to work around this other than to insert the teardown handler as the last one – with a proper test to make sure things keep working. I'll do that and make a release soon. The worst case scenario is this is changed in the future but I guess we'll have to cross that bridge once we reach it. Permalink to the referenced Flask source code piece for historical visibility. |
Well, inserted as the first one, but yes, same concept. :) If I find the time in the next day or so, I can open a PR with these changes. Like you said, we can cross the bridge when we come to it. |
Sorry, yes, the first one. I'll patch this today, but thanks anyway for wanting to help with it. |
The fix has just been released in version 0.12.1. |
Using version |
What versions of Python and Flask are you using? Can you provide a piece of code to reproduce the incorrect behavior? I'm afraid the issue will be difficult to narrow down otherwise. |
Python is |
I think this reproduces the issue so no need for further investigation on your part:
|
Flask-Injector 0.12.3 I just released should fix this for you. |
Can confirm its working now. Many thanks for this amazingly fast fix! |
If I have a request scoped variable, I cannot inject it into a
teardown_request
handler. If I do, I get the following exception.After some digging, it seems that this is because Flask executes teardown handlers in the reverse order that they are inserted (see relevant source). Because flask_injector inserts the request scope clean up as a teardown handler, after all others are inserted, flask_injector's handler is run before mine.
I was able to hack around this by, after setting up my
FlaskInjector
runningapp.teardown_request_funcs[None].reverse()
. This is not a great solution, IMO. I'm hesitant to submit a PR that just inserts this teardown handler as the first one, as the Flask docs don't seem to guarantee that the handlers are called in reverse order; it seems like an implementation detail.What follows is an (admittedly silly) proof of concept that produces the above exception on request.
The text was updated successfully, but these errors were encountered: