You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Templating middleware initializes request context in after_server_start handler however
at this point the server is already serving requests.
Additional context __request__ is not a good name for this, it implies that it is a magic variable typically belonging to Python itself, PEP8 suggests never inventing new ones.
I am guessing the intent was name mangling, but that only works on class attributes when prefixed with two underscores.
Load the context with before_server_start handler instead?
On closer inspection it does not appear that __request__ is used at all and this whole initialization chain can be stripped out?
Mitigations
# I need templates: Reach into the `app._future_middleware` and fish out the broken function.>>>app._future_middleware[0].middleware.__module__'sanic_ext.extensions.templating.extension'# I don't need templates: Initializing extensions manually without builtins, then picking the modules used.>>>app.extend(built_in_extensions=False, extensions=[HTTPExtension, OpenAPIExtension])
The text was updated successfully, but these errors were encountered:
Templating middleware initializes request context in after_server_start handler however
at this point the server is already serving requests.
That is not true. Requests are not yet being received when after_server_start runs. The server is running and is bound to a socket, but it is not yet receiving anything.
Start the Sanic server and immediately make a request.
Some sample code would be helpful.
I am guessing the intent was name mangling
Nope.
can be stripped out?
Yup. Functionality has been replaced and is now part of Sanic itself as of v22.6: Request.get_current()
Describe the bug
Templating middleware initializes request context in
after_server_start
handler howeverat this point the server is already serving requests.
Screenshots
To Reproduce
Start the Sanic server and immediately make a request.
Expected behavior
When the server is ready to handle requests the context variable should already be set.
Environment
Google Cloud Platform / Cloud Run / docker image: python:3.10-slim-bullseye
Dependencies handled by Poetry
Additional context
__request__
is not a good name for this, it implies that it is a magic variable typically belonging to Python itself, PEP8 suggests never inventing new ones.I am guessing the intent was name mangling, but that only works on class attributes when prefixed with two underscores.
Load the context with
before_server_start
handler instead?On closer inspection it does not appear that
__request__
is used at all and this whole initialization chain can be stripped out?Mitigations
The text was updated successfully, but these errors were encountered: