Open digitalfiz opened 1 year ago
@ahopkins here is the issue I was talking about :)
My initial reaction is that it sounds like maybe "lifespan.startup"
is being called multiple times.
Just curious, what asgi server are you running this with?
Mangum basically acts like a translator between an aws lambda request structure and what an asgi server would send to Sanic or FastAPI or any of the others.
I ran into the same problem yesterday and below is my quick hack to make it work. This has only been tested locally with SAM.
The problem is that Sanic expects that scope has raw path in scope["raw_path"]
, which is set to None by default on the handler construction. The solution is to overwrite __call__
in Mangum handler to craft the scope and add raw path.
This issue is from July 25, it should have been fixed by now. Am I missing something?
class MangumWrapper(mangum.Mangum):
def __init__(self, app: Sanic[Config, SimpleNamespace]) -> None:
super().__init__(app)
def __call__(self, event: LambdaEvent, context: LambdaContext) -> dict:
handler = self.infer(event, context)
crafted_scope = {**handler.scope, "raw_path": handler.scope["path"].encode()}
with ExitStack() as stack:
if self.lifespan in ("auto", "on"):
lifespan_cycle = LifespanCycle(self.app, self.lifespan)
stack.enter_context(lifespan_cycle)
http_cycle = HTTPCycle(crafted_scope, handler.body)
http_response = http_cycle(self.app)
return handler(http_response)
On a cold start I get this:
And this on a warm call:
I don't know what versions of which are compatible with each other but the latest of each is not. I used the example from https://mangum.io/asgi-frameworks/#sanic. I had to add a name to the
Sanic()
instance because they require that now but I can not figure out what else might now be required to work with Mangum or if Mangum needs interal adjustments to work with Sanic.Here is my full example of what I am using: