Monitors and exits
A link ties two failures together. In Erlang a monitor does not: it watches a process from outside and reports its death as a message. The watcher decides what to do and keeps running, which is why a monitor is the tool for a client, a test or a request that waits for a worker.
monitor/2
erlang:monitor(process, Pid) answers a reference, a fresh value that names this one watch. The watched process is not told and is not affected; the watcher only receives a message if it dies. A reference cannot be printed in a fixed transcript because it is new on every call, so the shell checks its type:
erl
1> is_reference(erlang:monitor(process, spawn(fun() -> receive stop -> ok end end))). true 2> erlang:demonitor(erlang:monitor(process, self())). true
The second line starts a monitor on the shell itself and drops it again. The reference is the key to the message, so a process that keeps several monitors keeps their references and matches on them.
DOWN messages
When the watched process dies, the watcher receives {'DOWN', Ref, process, Pid, Reason}. The reference says which watch ended, the pid says which process, and the reason is the exit reason. This program watches a child that fails and prints the reason:
watcher.erl
-module(watcher). -export([main/0]). main() -> Pid = spawn(fun() -> receive go -> exit(boom) end end), Ref = erlang:monitor(process, Pid), Pid ! go, receive {'DOWN', Ref, process, Pid, Reason} -> io:format("~p~n", [Reason]) end.
erlc watcher.erl
erl -noshell -eval 'watcher:main(), halt().'
boom
The watcher survives the failure, which is the whole difference from a link. A monitor also reports a normal exit, with reason normal, so a client can tell a worker that finished from one that crashed.
Watching a dead process
Monitoring a pid that is already dead is safe: the runtime answers a reference and sends the DOWN at once with reason noproc. A client can register its watch after the worker started without a race, though a name or a registry is still the cleaner way to find it:
dead.erl
-module(dead). -export([main/0]). main() -> Pid = spawn(fun() -> ok end), timer:sleep(10), Ref = erlang:monitor(process, Pid), receive {'DOWN', Ref, process, Pid, Reason} -> io:format("~p~n", [Reason]) end.
erlc dead.erl
erl -noshell -eval 'dead:main(), halt().'
noproc
The reason noproc is the answer to "was it ever there"; a worker that ran and finished answers normal. A program that treats the two the same hides a missing server behind a successful shutdown.
The DOWN that stays
erlang:demonitor(Ref) stops a monitor but leaves a DOWN message that already arrived in the mailbox. The flush option removes that message too, and without it a receive later in the program can pick up a death it already handled. This program demonitors after the child died and finds the message still there:
stale.erl
-module(stale). -export([main/0]). main() -> Pid = spawn(fun() -> ok end), Ref = erlang:monitor(process, Pid), timer:sleep(10), true = erlang:demonitor(Ref), Result = receive {'DOWN', Ref, process, Pid, Reason} -> {stale, Reason} after 0 -> clean end, io:format("~p~n", [Result]).
erlc stale.erl
erl -noshell -eval 'stale:main(), halt().'
{stale,normal}
With erlang:demonitor(Ref, [flush]) the same program prints clean. The rule is to demonitor with flush whenever the code might have stopped waiting before the message arrived, which is most of the time.
Links or monitors
A link is structure: the parent and the child are one failure unit, and the parent wants to die with the child because the supervisor will restart both. A monitor is observation: a client watches a worker, a test watches the process under test, and the watcher decides what the death means. The supervisors lesson uses links, and the message protocols lesson uses a monitor for the call that must not hang.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
erlang:monitor(process, Pid)answer?Answer
A reference
It answers a monitor reference, a fresh value that identifies this monitor.
-
How is a monitor different from a link?
Answer
It is one-directional and needs no agreement
The watcher observes; the watched process is not affected by the monitor.
-
What does the watcher receive when the monitored process dies?
Answer
{'DOWN', Ref, process, Pid, Reason}A monitor delivers a
DOWNtuple with the reference, the pid, and the reason. -
Why match on the reference in a DOWN message?
Answer
To tell several monitors apart
Each monitor has its own reference, so the reference says which watch has ended.
-
What does this module print?
-module(q1). -export([main/0]). main() -> Pid = spawn(fun() -> receive go -> exit(boom) end end), Ref = erlang:monitor(process, Pid), Pid ! go, receive {'DOWN', Ref, process, Pid, Reason} -> io:format("~p~n", [Reason]) end.Answer
boomThe child exits with reason
boom, and the DOWN message carries it. -
What does this module print?
-module(q2). -export([main/0]). main() -> Pid = spawn(fun() -> ok end), timer:sleep(10), Ref = erlang:monitor(process, Pid), receive {'DOWN', Ref, process, Pid, Reason} -> io:format("~p~n", [Reason]) end.Answer
noprocThe process was already dead, so the monitor fires at once with
noproc. -
What does this module print?
-module(q3). -export([main/0]). main() -> Pid = spawn(fun() -> ok end), Ref = erlang:monitor(process, Pid), timer:sleep(10), true = erlang:demonitor(Ref), Result = receive {'DOWN', Ref, process, Pid, _} -> stale after 0 -> clean end, io:format("~p~n", [Result]).Answer
staleDemonitor without
flushleaves the queued DOWN, so the receive still finds it. -
What does this module print?
-module(q4). -export([main/0]). main() -> Ref = erlang:monitor(process, self()), io:format("~p~n", [erlang:demonitor(Ref)]).Answer
truedemonitor/1answerstruefor a live monitor. -
When should you use a link instead of a monitor?
Answer
Inside a supervision tree that must fail as one unit
Links tie failures together, which is what a supervisor wants from its children.
-
Why does
watcher:main/0survive the failure of its child?Answer
A monitor reports the death but does not kill the watcher
A monitor is one-directional observation, so only a message arrives.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Автор статьи: Arthur Isaev