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

How to get notified for any crash from any linked process?

我在我的应用程序中通过 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.

感谢@7stud,我最终使用的解决方案是在 Elixir 记录器中激活 SASL 和 OTP 登录。

如果您使用 mix,您可以在 config/config.exs 中添加以下行:

config :logger,
  handle_otp_reports: true,
  handle_sasl_reports: true

有关详细信息,请参阅 elixir logger documentation