Name
receive_with_timeout
Brief Description
All receive expressions should be accompanied by an after expressions.
Problematic code
receive
onhook ->
disconnect(),
idle();
{connect, B} ->
B ! {busy, self()},
wait_for_onhook()
end
Correct code
receive
onhook ->
disconnect(),
idle();
{connect, B} ->
B ! {busy, self()},
wait_for_onhook()
after
60_000 ->
exit(nothing_received)
end
Rationale
A receive block without a timeout will wait indefinitely if no matching message arrives; by making your timeout explicit you:
- avoid hanging processes
- improve testability (deterministic behaviour under test failure conditions)
- ease debug and recovery
- can implement retry and self-healing
- potentially avoid denial-of-service scenarios where waits are exploited