liveBook · Manning

Process link

As explained in Elixir in Action, Third Edition

A process link is a bidirectional association between exactly two processes that propagates abnormal termination notifications between them.

A process link is a bidirectional association between exactly two processes. When one process terminates abnormally, the other receives an exit signal containing its PID and exit reason; by default, the linked process terminates too. Normal termination uses the reason :normal and does not trigger this failure propagation. section 8.2.1

Use links to shape failure behavior across concurrent systems:

  • Create a link explicitly with Process.link/1, or create one while spawning with spawn_link/1. section 8.2.1
  • Connect dependent processes so a failure terminates the surrounding structure instead of leaving dangling workers behind. section 8.3.6
  • Trap exits with Process.flag(:trap_exit, true) when a process must receive {:EXIT, pid, reason} as a message and react instead of terminating. section 8.2.1
  • Prefer start_link conventions for GenServer processes that supervisors manage; bidirectional links also take children down when their supervisor terminates. section 8.3.1

Propagating a crash

A parent process can spawn_link a worker, let the worker raise, and observe that the parent is also taken down by default. With exit trapping enabled, the parent instead receives the worker's exit notification and can decide what to do. section 8.2.1

iex(1)> spawn(fn ->
          spawn_link(fn ->       #1
            Process.sleep(1000)
            IO.puts("Process 2 finished")
          end)
 
          raise("Something went wrong")
        end)

Runnable IEx code demonstrates a linked worker sleeping while its parent raises, making abnormal termination propagation visible. section 8.2.1

Links can form transitive structures: a crash may propagate through every connected process. This is useful for tightly coupled systems, but a single worker failure can have an unnecessarily wide impact. Supervision trees refine this arrangement by choosing how failures should affect the rest of the system. Links also work across nodes, where crashes, BEAM or machine failures, and lost network connections produce failure notifications. section 8.3.6 section 12.1.4

How Elixir in Action, Third Edition covers it

How deep it goes
Explains creation, exit signals, trapping, transitive failure, and production trade-offs while deferring finer-grained isolation improvements.
Examples
Demonstrates spawn_link crashes, trapped exit messages, supervised GenServer startup, and linked to-do application processes.
What you can do after
Build it from scratch with Process.link/1 or spawn_link/1, then configure exit trapping and supervised process lifecycles.
How it connects
Connects processes, exit signals, monitors, supervisors, and supervision trees.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.