GenServer.terminate
terminate, go back to GenServer module for more information.
Specs
terminate(reason, state :: term()) :: term() when reason: :normal | :shutdown | {:shutdown, term()} | term()
Invoked when the server is about to exit. It should do any cleanup required.
reason is exit reason and state is the current state of the GenServer.
The return value is ignored.
terminate/2 is called if the GenServer traps exits (using Process.flag/2)
and the parent process sends an exit signal, or a callback (except init/1)
does one of the following:
- returns a
:stoptuple - raises (via
Kernel.raise/2) or exits (viaKernel.exit/1) - returns an invalid value
If part of a supervision tree, a GenServer will receive an exit
signal when the tree is shutting down. The exit signal is based on
the shutdown strategy in the child's specification, where this
value can be:
:brutal_kill: theGenServeris killed and soterminate/2is not called.a timeout value, where the supervisor will send the exit signal
:shutdownand theGenServerwill have the duration of the timeout to terminate. If after duration of this timeout the process is still alive, it will be killed immediately.
For a more in-depth explanation, please read the "Shutdown values (:shutdown)"
section in the Supervisor module.
If the GenServer receives an exit signal (that is not :normal) from any
process when it is not trapping exits it will exit abruptly with the same
reason and so not call terminate/2. Note that a process does NOT trap
exits by default and an exit signal is sent when a linked process exits or its
node is disconnected.
Therefore it is not guaranteed that terminate/2 is called when a GenServer
exits. For such reasons, we usually recommend important clean-up rules to
happen in separated processes either by use of monitoring or by links
themselves. There is no cleanup needed when the GenServer controls a port (for example,
:gen_tcp.socket) or File.io_device/0, because these will be closed on
receiving a GenServer's exit signal and do not need to be closed manually
in terminate/2.
If reason is neither :normal, :shutdown, nor {:shutdown, term} an error is
logged.
This callback is optional.