Linking Processes in Supervision

Learn how to link processes in supervision.

We'll cover the following


Supervisors may sound magical, but there are two simple, practical mechanisms behind their magic: linking processes and trapping exits. We’ll see how they work together to make supervisors work.

The key to process supervision is for one process to be able to link to other processes and know when those other processes crash. Beyond that, the supervising process needs to keep running if it gets crash notifications from the linked processes.

When one process in a group of linked processes crashes, it sends a special message called an exit signal to all its the other linked processes. Any process that receives an exit signal will also exit and send an exit signal to all its other processes, forming a chain reaction of exits.

This chain reaction makes it hard for the supervisor processes not to crash when one of the processes they’re monitoring crashes.

The mechanism to fix this is called trapping exits. When a process traps exits, it transforms exit signals into regular messages that end up in the process’s mailbox. This process can then handle those regular messages just as it would any other. That’s the key to making supervisors work.

Those are nice words, and they help us understand supervisors from one angle, but digging in and watching processes interact in the console will deepen our understanding. This is what we’re about to do.

Send and receive messages

We’re going to see how unlinked processes behave when they exit. Then, we do the same for linked processes and for linked processes where one is trapping exits.

Much of this work involves passing and retrieving messages directly. We’ll use send/2 to send messages and receive/1 to pull them from a process’s mailbox.

Let’s take a quick practice run with these functions in IEx.

Get hands-on with 1200+ tech skills courses.