elixir - 如何从任何链接进程获得任何崩溃通知?

标签 elixir erlang-otp

我通过 Supervisor、Dynamic supervisor 在我的应用程序中启动多个 (genserver) 进程,有时甚至只是一个直接的 start_link。当我使用 iex -S mix 启动应用程序时,其中一些可能会崩溃而不会通知我。如果我自己没有发现,如何确保我收到来自任何进程的所有崩溃通知?在跟踪所有事件的同时围绕“让进程崩溃”有哪些好的做法?

XXX.start_link(ws_uri, __MODULE__, state, [{:name, MyModule}])

最佳答案

来自GenServer docs :

terminate/2 is called if a callback (except init/1) does one of the following:

-returns a :stop tuple
-raises
-calls Kernel.exit/1
-returns an invalid value
-the GenServer traps exits (using Process.flag/2) and the parent process sends an exit signal

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,
...
...
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.

关于elixir - 如何从任何链接进程获得任何崩溃通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64485373/

相关文章:

concurrency - 在 Erlang 中将消息路由到 PID

erlang - 是erlang :send_after/3 and timer:send_after/3 intended to behave differently?

elixir - 对 Elixir 中的重复字母进行分组或计数

elixir - 在 Phoenix Framework 表单中,如何使用变更集将belongs_to 关系设置回 null?

erlang - 从命令行启动 erlang 应用程序的问题

Erlang OTP 主管 gen_tcp - {error,eaddrinuse}

concurrency - Elixir - 受监督的进程似乎会阻止程序执行

jquery - Phoenix channel 从项目外的客户端发送消息

elixir - Ecto.Query.from/2 的第二个参数

elixir - 带有 do-block 的关键字参数