Erlang Medium · Lesson 1 · On the house

Links and exit signals

link, spawn_link, trap_exit, and how crashes propagate between processes. Lesson 1 is free — on the house.

Lessons · Erlang Medium · Lesson 1 of 10 · On the house

Links and exit signals

link, spawn_link, trap_exit, and how crashes propagate between processes.

Illustration for Erlang
Lesson 1 is on the house.

A link is a bidirectional bond: if one process dies abnormally, an exit signal kills the other too. spawn_link/1 spawns and links atomically.

Normal exits (normal reason) don't kill linked processes.

A process that calls process_flag(trap_exit, true) receives exit signals as messages {'EXIT', Pid, Reason} instead of dying. Supervisors work this way.

exit(Pid, kill) is untrappable and always kills.

Links make groups of processes fail together, so no half-alive state lingers.

trap_exit

main(_) ->
    process_flag(trap_exit, true),
    Crasher = spawn_link(fun() -> timer:sleep(10), exit(boom) end),
    receive {'EXIT', Crasher, Reason} -> io:format("trapped ~p~n", [Reason]) end,
    Normal = spawn_link(fun() -> ok end),
    receive {'EXIT', Normal, R2} -> io:format("exit ~p~n", [R2]) end,
    Victim = spawn(fun() -> process_flag(trap_exit, true), receive _ -> ok end end),
    exit(Victim, kill),
    timer:sleep(10),
    io:format("victim alive: ~p~n", [is_process_alive(Victim)]).
Match this when you type.

Quiz

What happens to a linked process when its partner crashes (no trapping)?

Quiz

What does trap_exit turn exit signals into?

Quiz

Which exit reason can't be trapped?

Quiz

Does a normal exit kill linked processes?

Check

Match the still for: Links and exit signals.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.