sysid / sse-starlette

BSD 3-Clause "New" or "Revised" License
545 stars 37 forks source link

can not exit async generator in shutdown-event when shutting down server with fast_api? #8

Closed yeus closed 3 years ago

yeus commented 4 years ago

fast api has "shutdown" event --> documentation <-- and I would like to use that to give the event generator a signal to exit its loop. But the shutdown event in fastapi never gets called when using sse-starlette because sse-starlette doesn't shutdown itself as its waiting for clients to disconnect. So I am getting the

Waiting for connections to close. (CTRL+C to force quit) Error which was also stated in the readme.

So, how would I do that? Is there anything available "out-ofthe-box" in starlette/fastapi?

sysid commented 3 years ago

Thank you @yeus for bringing this to attention. I will look into it. If it is time critical, pull requests are welcome, of course.

yeus commented 3 years ago

thanks @sysid. Its not time critical, just very annoying in development. For example hot-code reloading doesn't work anymore in fast-api as the server restart is blocked while we are waiting for the connection to close. And as soon as we restart, the client tries reconnecting to the sse connection because it never got a message to disconnect from it.

I am not very deep into the code base so can't immediately send you a pull request. But I am working my way into it.. so we'll see if I can come up with something. Will post here if I do, but probably won't be able.

sysid commented 3 years ago

I created a branch for fastapi: feat/stop which should address your issue. In any case CTRL-C now stops a long running data generator gracefully and lets uvicorn shutting down.

It turns out that plugging into the "shutdown" event is not the way to go, since it is only triggered when all tasks are done and all connections are closed.

So the data generator needs to get the signal via different means, e.g. by adding a signal handler onto the event-loop. However this requires monkey-patching the uvicorn signal handler. It also creates a dependency on uvicorn (which is acceptable because it can be handle as soft dependency).

Feedback welcome.

yeus commented 3 years ago

@sysid awesome thx, will try it out and report ;)

sysid commented 3 years ago

I have merged it into master -> 0.6.0.

paxcodes commented 3 years ago

@sysid The latest public release is 0.4.0 --- is 0.6.0 a typo or is this a future release? If a future release, when will you make it public? Anything I can do to make that happen?

sysid commented 3 years ago

Not sure what you mean paxcodes. On PyPi the latest version is available: https://pypi.org/project/sse-starlette/.

paxcodes commented 3 years ago

Ah. I was looking at the repo's "releases" tab 🤦🏻‍♀️

Sorry about that and thank you! 👍🏼