Trap exits to ensure linked process exits?

When you trap exits, exit signals arriving to a process are converted to {'EXIT', From, Reason} messages, which you normally can receive like any other message. However, your process is a GenServer, and GenServers apparently will crash automatically when they receive an EXIT message, no matter what: Trying to understand GenServer terminate/2 behavior when trapping exit - #2 by michalmuskala

So trapping exits doesn’t really work for GenServers, in the sense that you can’t trap exits and expect your process not to crash when the parent terminates. In fact, when you trap exits on your GenServer it always terminates when the parent terminates, as you observed in your experiments. I actually had no clue it worked like this, I just found out and was very surprised.

3 Likes