Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

on_response hook don't receive the same injected dependency as on_request #490

Closed
pslacerda opened this issue Apr 26, 2018 · 5 comments
Closed

Comments

@pslacerda
Copy link
Contributor

pslacerda commented Apr 26, 2018

Edited on_error -> on_response.

For some reason some objects injected on_request() isn't the same injected on on_response(), but other components like WSGIEnviron are. This happened on an exception context.

class AppOpticsTracerHooks:
    @staticmethod
    def on_request(dependency: Dependency, environ: WSGIEnviron) -> None:
        dependency.attr = 1
        environ['attr'] = 1

    @staticmethod
    def on_response(dependency: Dependency, host: Host,
                 request: Request, response: Response) -> Response:
         # after a exception happened on the handler
         assert environ['attr'] == 1  # ok
         assert dependency.attr == 1  # fails
@pslacerda pslacerda changed the title on_error hook don't receive the same injected dependency as on_request on_response hook don't receive the same injected dependency as on_request Apr 26, 2018
@pslacerda
Copy link
Contributor Author

pslacerda commented Apr 26, 2018

Seems that for each injector.run() a new initial state is provided. Maybe could the state be preserved between injector.run() calls in the same App.__call__() with something like the following?

result, state = injector.run()

@pslacerda
Copy link
Contributor Author

Forgot the last comment. I have no idea what I was talking about.

I wanted to say to resolve_functions() use the actual state together with the initial state if possible. Maybe can this actually improve the performance and fix this issue? Or there are any Component that needs to be always reinstantiated?

@tomchristie
Copy link
Member

For some reason some objects injected on_request() isn't the same injected on on_response(), but other components like WSGIEnviron are. This happened on an exception context.

I'd consider this a valid bug yup - they ought to get the same state, if it has already been created.

@pslacerda
Copy link
Contributor Author

Seems that it you already fixed it somewhere between this issue and today. state is the same between hooks invokations.

state = {

@tomchristie
Copy link
Member

@pslacerda Great, thanks for confirming.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants