-
-
Notifications
You must be signed in to change notification settings - Fork 731
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
Enable graceful shutdown when running multiple workers and sending a SIGTERM #853
Conversation
bb4234f
to
93d7596
Compare
Add a signal event to the run_server context manager
I rebased this against master with new tests, adapted the run_server context manager to cope with how signals are handled in this version. I'm quite happy with it, except for the 3.6 drop, but maybe we can keep it for after the handler change, since I dropped 3.6 because I'm using server.serve_forever() In any case, after or before, the logic at hand wont change much and it's a pretty neat addition. |
@euri10 This is looking interesting, but I must say that's quite a lot of code and changes to go through. It seems you're kind of doing the same trick (using a multiprocessing event) three times. Would there be any chance you could start with only one bit, say hot reload (or whatever is easiest to add first), so we can see more easily what the various pieces are? Just asking, if it's all interlinked then okay, I can try and take the time to sit down and go through this 😄 but if there are ways to scale things down in increments, that would be interesting too... |
@euri10 Taking a closer look at this PR, I view the introduction of I have a feeling a prerequisite for this would be to start moving asyncio-specific pieces out of |
Ok will wait then, not sure sure how to 🍕 slice it right now but eventually this will come. |
I've also given the changes a review, it looks good to me. Also it's a net addition of only 30 lines.
This is a major win for uvicorn infrastructure and solves a serious pain point.
This should NOT be a concern because:
Thank you @euri10 for the great work here and @florimondmanca for the feedback!! We should do what we can to move this feature forward. |
Is there an ETA on this merge? |
I am also suffering this same problem. Cannot exit properly with sigterm if workers are used on Windows. Is there a fix? |
@euri10 what was wrong? a better patch is coming? |
Fixes #852
This is quite a huge review, so happy to explain anything that might look not easily understandable.
As @florimondmanca noticed, it takes inspiration from hypercorn by handling the multiple process shutdown through an external
multiprocessing.Event
that defines an infinite loop. That loop will break when that external event is set.Where it deviates from hypercorn implementation is in the reloader case : I took the view that
--reload
is a particular case of--workers
where there is arestart
method that is invoked when a file is modified.If necessary I can comment on the diff the main takeaways, let me know. It took me quite a while to get it right and hopefully it's working, I hope there are not leftovers from previous attempts.
one pretty big caveat but it's maybe doable, it removes support for python 3.6 : I'm usingnot using server.serve_forever in factserver.serve_forever()
which appears in 3.7 and couldn't for this find a way to handle its absence in 3.6 (there is another case where it was quite easy to adapt for 3.6)log of a gentle sigterm