Skip to content
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

ASGI on_shutdown is not called #200

Closed
Tobiaqs opened this issue Oct 3, 2020 · 0 comments
Closed

ASGI on_shutdown is not called #200

Tobiaqs opened this issue Oct 3, 2020 · 0 comments
Assignees
Labels

Comments

@Tobiaqs
Copy link

Tobiaqs commented Oct 3, 2020

I have the following ASGI application:

sio = socketio.AsyncServer()

def on_startup():
    print("on_startup()")
def on_shutdown():
    print("on_shutdown()")

app = engineio.ASGIApp(sio, on_startup=on_startup, on_shutdown=on_shutdown)

When I start the application with uvicorn main.app:app:

INFO:     Started server process [340374]
INFO:     Waiting for application startup.
on_startup()
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
^CINFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [340374]

It seems like the on_shutdown method is not being called.

After some digging in the ASGIApp source code I found that self.lifespan is only awaited once (but there are two events to be received).

I updated my code as such:

class WaitTwiceASGIApp(engineio.ASGIApp):
    async def lifespan(self, receive, send):
        await super().lifespan(receive, send)
        await super().lifespan(receive, send)

app = WaitTwiceASGIApp(sio, on_startup=on_startup, on_shutdown=on_shutdown)

Now the result is this:

INFO:     Started server process [340470]
INFO:     Waiting for application startup.
on_startup()
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
^CINFO:     Shutting down
INFO:     Waiting for application shutdown.
on_shutdown()
INFO:     Application shutdown complete.
INFO:     Finished server process [340470]

I don't know if awaiting ASGIApp.lifespan twice is a solution that conforms the ASGI standard but so far it seems to work on all ASGI servers I've tried.

I've tried shutting down uvicorn with kill to see if it'd make a difference compared to Ctrl-C, but it didn't seem to matter.

My versions are: python 3.8.5, python-socketio==4.6.0, python-engineio==3.13.2

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

No branches or pull requests

2 participants