liveBook · Manning

Graceful shutdown

As explained in Elixir in Action, Third Edition

Graceful termination is the controlled shutdown of a process that allows it to perform final cleanup before exiting.

Graceful shutdown terminates a process or supervision subtree in a controlled way, allowing final cleanup before exit. A supervisor asks children to stop, and nested supervisors propagate termination through their own trees. section 9.1.6

Apply graceful shutdown with these operational boundaries:

  • Enable exit trapping in a GenServer during init/1 when cleanup must run; graceful termination then invokes terminate/2. section 9.1.6
  • Set the child specification's shutdown option to a millisecond timeout, :infinity, or :brutal_kill. section 9.1.6
  • Expect forceful termination when cleanup exceeds the configured timeout; the default is 5,000 milliseconds for workers and :infinity for supervisors. section 9.1.6

A cleanup-aware worker

Configure a GenServer to trap exits from init/1, then place final cleanup in terminate/2. Its supervisor can wait for that cleanup with shutdown: 5_000, wait indefinitely with :infinity, or skip the graceful phase with :brutal_kill. section 9.1.6

Figure 9.4 Supervision tree
CH09_F04_Juric3

Trace how termination flows from a top-level supervisor through its immediate children and nested supervision subtrees. section 9.1.6

How Elixir in Action, Third Edition covers it

How deep it goes
Explains propagation, GenServer callbacks, exit trapping, timeout choices, and forceful termination boundaries.
Examples
Uses a top-level supervisor, nested supervisor trees, GenServer terminate/2, and shutdown values including 5_000, :infinity, and :brutal_kill.
What it assumes
Assumes supervision trees, GenServer callbacks, child specifications, and process exit signals.
What you can do after
Operate/configure supervised workers with cleanup callbacks and bounded, indefinite, or immediate shutdown behavior.
How it connects
Connects supervision trees, GenServer, child specifications, exit trapping, and exit signals.

Also explained in

Compare how 4 books explain graceful shutdown →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.