Links and monitors

A process in Elixir is isolated: it does not share memory with its neighbours and nobody watches it by default. Two tools tell one process that another one ended. A link connects two processes in both directions and sends an exit signal; a monitor is a one-way watch that sends a message. The processes lesson used a monitor without a name for it, and this page shows what each tool is for.

A link is two-way

spawn_link/1 starts a process and links it to the caller, and Process.link/1 links two processes that already run. A link carries an exit signal: when one side ends, the other side receives the same reason, and by default it ends too. A process that traps exits receives the reason as a message instead:

link.exs

Process.flag(:trap_exit, true) pid = spawn_link(fn -> exit(:boom) end) receive do {:EXIT, ^pid, reason} -> IO.inspect(reason) end IO.puts(Process.alive?(pid))

elixir link.exs

:boom
false

The child ended with the reason :boom, the link delivered it as an {:EXIT, pid, reason} tuple, and the parent stayed alive because Process.flag(:trap_exit, true) turned the signal into a message. Without that flag the same program would end with the child, which is the behaviour a supervised tree relies on.

Trapping the exit

The flag is set by the process that wants to survive, and it applies to every link of that process. A child that ends normally sends the reason :normal the same way, so a trapping parent sees every end of a linked process, not only the crashes:

normal.exs

Process.flag(:trap_exit, true) pid = spawn_link(fn -> :ok end) receive do {:EXIT, ^pid, reason} -> IO.inspect(reason) end

elixir normal.exs

:normal

A trapping process is responsible for the messages it asked for: a receive that matches only one shape leaves the others in the mailbox. Process.flag/2 returns the old value, which is how a library restores the flag of its caller after a short section.

A monitor is one-way

Process.monitor/1 watches a process without linking to it. The watching process is not affected when the other one ends; it receives one :DOWN message with the reference the call returned. A monitor therefore suits a caller that wants to know the outcome without sharing the fate of the worker:

monitor.exs

pid = spawn(fn -> exit(:boom) end) ref = Process.monitor(pid) receive do {:DOWN, ^ref, :process, ^pid, reason} -> IO.inspect(reason) end

elixir monitor.exs

:boom

The reference in the message is the one Process.monitor/1 answered, which is why the pattern pins it: several monitors can be open at once, and the reference says which process ended. Process.demonitor(ref, [:flush]) cancels a monitor and removes a message that is already in the mailbox.

The exit that cannot be trapped

One reason ignores both the flag and the link: :kill. It ends the target process at once, and the linked process sees :killed rather than the reason the target would have chosen. The distinction matters because a stuck process cannot refuse to die:

kill.exs

Process.flag(:trap_exit, true) pid = spawn_link(fn -> Process.sleep(:infinity) end) Process.exit(pid, :kill) receive do {:EXIT, ^pid, reason} -> IO.inspect(reason) end

elixir kill.exs

:killed

The sleeping child could not hold the kill, and the trapping parent received the result. A supervisor uses the same call when a child ignores a normal shutdown, which is why a supervised tree always ends, even when one of its processes is stuck.

The mistake: waiting for DOWN on a link

The two tools send different messages, and a reader who mixes them waits for a message that never arrives. A link sends :EXIT; only a monitor sends :DOWN:

wrongmsg.exs

Process.flag(:trap_exit, true) pid = spawn_link(fn -> exit(:boom) end) receive do {:DOWN, ^pid, :process, _, reason} -> IO.inspect(reason) after 100 -> IO.puts("a link sends EXIT, not DOWN") end

elixir wrongmsg.exs

a link sends EXIT, not DOWN

The program did not hang only because of the after clause; without it the :EXIT message would sit in the mailbox while the process waited for a shape that no link produces. The fix is the pattern the tool sends, or a monitor when a one-way watch is what the program needs.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a link between two processes carry?

    Answer

    An exit signal that ends both sides by default

    A link is bidirectional: when one side ends, the other receives the reason and ends too unless it traps exits.

  2. What does Process.flag(:trap_exit, true) change?

    Answer

    It turns an exit signal from a link into an EXIT message

    With the flag set, an exit signal arrives in the mailbox as {:EXIT, pid, reason} instead of ending the process.

  3. Which message does a monitor send when the watched process ends?

    Answer

    {:DOWN, ref, :process, pid, reason}

    A monitor is one-way and answers one DOWN message with the reference the monitor call returned.

  4. Why can Process.exit(pid, :kill) not be trapped by the target?

    Answer

    Because :kill ends the process at once and bypasses trap_exit

    :kill is the one reason that ignores trap_exit; the linked side sees :killed.

  5. What does this program print? Process.flag(:trap_exit, true) pid = spawn_link(fn -> exit(:boom) end) receive do {:EXIT, ^pid, reason} -> IO.inspect(reason) end

    Answer

    :boom

    The parent traps the exit, so the reason of the child arrives as a message.

  6. What does this program print? Process.flag(:trap_exit, true) pid = spawn_link(fn -> :ok end) receive do {:EXIT, ^pid, reason} -> IO.inspect(reason) end

    Answer

    :normal

    A link signals every end of the linked process, a normal return included.

  7. What does this program print? pid = spawn(fn -> exit(:boom) end) ref = Process.monitor(pid) receive do {:DOWN, ^ref, :process, ^pid, reason} -> IO.inspect(reason) end

    Answer

    :boom

    The monitor reports the reason the process ended with, and the caller stays alive.

  8. What does this program print? Process.flag(:trap_exit, true) pid = spawn_link(fn -> Process.sleep(:infinity) end) Process.exit(pid, :kill) receive do {:EXIT, ^pid, reason} -> IO.inspect(reason) end

    Answer

    :killed

    The target cannot trap :kill, and the link reports :killed to the parent.

  9. A program traps exits and waits for {:DOWN, ...} after spawn_link. What happens?

    Answer

    The EXIT message arrives, and the DOWN wait times out

    A link produces EXIT, not DOWN; only Process.monitor/1 produces DOWN.

  10. When is a monitor the better tool than a link?

    Answer

    When the watcher only needs to know the outcome and must survive it

    A monitor is one-way: the watcher learns that the worker ended without sharing its fate.

Автор статьи: Arthur Isaev