Shuting down gracefully?

What is the proper way of postponing halting of running application on exit signal to shut down it gracefully?

Let's say I want to store the session after the user enters CTRL+C, before exiting.

Any difference in case of using tokio?

You'll likely want to install a signal handler.



In general, proper shutdown is a somewhat complex topic, as it is necessary application specific. But there are a bunch of useful ideas:

What is the source of shutdown? Typical sources are:

  • explicit shutdown request (ctrl+c)
  • implicit request via disconnection of the primary client (eg, your program's stdin closes)
  • internal emergency shutdown (some task/thread in application panicked, we want to shutdown the rest)
  • external abort (SIGKILL, or the power going down)

How components learn about shutdown?

  • all components might watch for explicit shutdown signal
  • or each component might provide natural, implict shutdown flow (typically channel's closure)
  • crash-only software -- robust architectures ensure that there's a single shutdown flow for both normal and abnormal shutdowns (aka, don't send Shutdown message over the channel, wait for channel's closure)
  • structured concurrency -- ideally, it should be impossible for any component to not learn about shutdown

What is the application-specific logic for graceful shutdown?

  • you typically want to stop accepting new requests, but process all outstanding requests

How data durability is ensured?

  • even if application is killed bypassing graceful shutdown, this shouldn't lead to disastrous consequences
  • if you are implementing something of truly high reliability, you probably want panic=abort

@H2CO3 @matklad thank you for elaborated answers and helpful links!

Full summary is here

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.