sanic-org / sanic-ext

Extended Sanic functionality
https://sanic.dev/en/plugins/sanic-ext/getting-started.html
MIT License
50 stars 35 forks source link

[Bug] Sanic serving request before injection complete #203

Open trungtin opened 1 year ago

trungtin commented 1 year ago

Describe the bug I did set app.config.INJECTION_SIGNAL = "http.handler.before", but when the server start, there is a split second where the handler runs before the injection finish, and it will throw an error with missing param.

In a normal situation, this wouldn't be an issue, but when using auto scaling (like Google Cloud Run), the instance is spun up to serve the request, then the first request will always result in a 500 error.

Expected behavior The handler will be called after the injection finish

Environment (please complete the following information):

trungtin commented 1 year ago

After posting this, I realize that it is probably due to my init function being asynchronous. So any suggestion how to deal with that?

@dataclass
class Realm:
    @classmethod
    async def from_header(cls, request: Request):
        # do something here
        return cls()

app.ext.add_dependency(Realm, Realm.from_header)