liveBook · Manning

Timeout

As explained in Elixir in Action, Third Edition

A timeout is a duration limit after which a waiting operation raises an error if its expected response has not arrived.

A timeout limits how long a process waits for a response. When the response does not arrive in time, the waiting operation raises an error instead of blocking indefinitely. section 7.3.3 section 6.2.3

Use timeouts to control responsiveness, while accounting for what happens to the underlying request:

  • GenServer.call/2 waits up to five seconds by default; GenServer.call/3 accepts a timeout in milliseconds. section 6.2.3
  • Task.await/1 also defaults to five seconds, while Task.await/2 accepts a different timeout. section 10.1.1
  • A timed-out call stops waiting but does not remove the request from the receiver's mailbox; the receiver may process it later. section 7.3.3
  • Shorter limits can improve responsiveness, but overloaded servers may still accumulate messages and consume memory. section 7.3.3

Waiting for concurrent work

Start independent queries with Task.async/1, then collect their results with Enum.map(tasks, &Task.await/1). Five simulated two-second queries complete together in about two seconds, subject to the await timeout. section 10.1.1

How Elixir in Action, Third Edition covers it

How deep it goes
Explains defaults, configuration, raised errors, and mailbox consequences without detailing timeout implementation internals.
Examples
Uses GenServer requests, long-running tasks, and five parallel simulated queries to demonstrate timeout configuration and waiting.
What it assumes
Assumes familiarity with processes, messages, GenServer calls, and asynchronous tasks.
What you can do after
Apply it via GenServer.call/3 or Task.await/2 to bound response waits and choose a responsiveness trade-off.
How it connects
Connects timeouts with GenServer, synchronous calls, Task.async, Task.await, process mailboxes, and system responsiveness.

Also explained in

Compare how 9 books explain timeout →

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.