2

I tried asking this question a few weeks ago, but included too many unnecessary details about what I am doing. This is my attempt to see if narrowing the question can get me some useful feedback.

I am running a number of servers via fastapi and uvicorn, and in many cases using the @app.on_event("shutdown") decorator for some code which should automatically run when the server is shut down. Initially, I was calling each of these servers in its own terminal, and pressing ctrl-c in that terminal was sufficient to reliably shut down the server and call the shutdown code. However, that is no longer practical for me to do, and so I am wondering, what other signals or procedures can be used to trigger the code under @app.on_event("shutdown") in fastapi with uvicorn. So far, I have confirmed that sending terminate or kill signals to the uvicorn process do not cause that code to be triggered on shutdown, and I am not sure what else would do the job. So, for other users of fastapi and uvicorn, what other than ctrl-c do you use to kill your servers?

jkflowers
  • 81
  • 6
  • 1
    Sending the `TERM` signal is a "harder" kill than what `ctrl+c` sends to the process. Have you tried sending `SIGINT` which is the same as hitting `ctrl+c`? `kill -SIGINT ` – MatsLindh Jun 17 '21 at 12:39
  • I will try that shortly and get back to you. Thanks! – jkflowers Jun 17 '21 at 13:15
  • Ok, right, I have tried and rediscovered why I did not try it previously, because signal.SIGINT is not supported on windows. The error message I get is ```ValueError: only SIGTERM, CTRL_C_EVENT and CTRL_BREAK_EVENT signals are supported on Windows``` – jkflowers Jun 18 '21 at 07:45
  • I realize I did not mention earlier, I have tried sending ```signal.CTRL_C_EVENT``` to the uvicorn server I want to end, but I find that it knocks out everything else I have running through that terminal window as well. – jkflowers Jun 18 '21 at 07:47
  • I am reading here (https://learn.microsoft.com/en-us/windows/console/ctrl-c-and-ctrl-break-signals) that windows by default passes this signal to all processes in a console. If I knew how to make it so only the target process received that signal, that would solve my problem completely. – jkflowers Jun 18 '21 at 07:51
  • Does this answer your question? [How to trigger clean shutdown of FastAPI/Uvicorn](https://stackoverflow.com/questions/67787621/how-to-trigger-clean-shutdown-of-fastapi-uvicorn) – Simba Jun 23 '21 at 10:34
  • @Simba haha that was my other attempt to ask this question. as you can see, I did not find an answer, and tried a slightly more specific question here. I suspect that this is just a fundamental limitation of these python packages, so I have been looking for solutions in windows, as mentioned above. I also am working on a little update that might make my main use for app.on_event("shutdown") unnecessary, in which case I could use the code I already have with only slight inconvenience. – jkflowers Jun 23 '21 at 14:07
  • @jkflowrers did you ever get an answer for this or what was your alternative solution? – Dude0001 Apr 02 '22 at 23:18
  • @Dude0001 No. As mentioned above, some other changes to my code design made all the on shutdown stuff I had unnecessary. I think I did get an answer on here about why shutdown events might not be something people who write servers care about very much, but I am not a professional software engineer and do not have the context to fully understand it. – jkflowers Apr 04 '22 at 06:21

0 Answers0