This is one possible solution to exercise 6-3 in the book Erlang Programming by F. Cesarini and S. Thompson (O'Reilly, 2009). We are asked to expand the functionality of the basic supervisor described in the chapter in three ways.
- Handle two types of child processes:
permanent
andtransient
. - Prevent the possibility of infinite restarts by throttling the restart rate.
- Allow child processes to be started and stopped after the supervisor has been started.
On my first attempt, I implemented the features in the order given. However, while testing the last of these, I encountered a problem.
I used the add_two
module during development. When I caused add_two:request/1
to crash by passing in an atom instead of an integer, the shell froze. I was expecting the supervisor to trap the exit, restart the child, and allow me to continue. What I was forgetting was that the shell client was waiting for a message from the crashed child that would never come. Thanks to Gleb Peregud on the erlang-questions
list for pointing this out and suggesting the introduction of a monitor to deal with this issue.
Besides employing a monitor, I added a stop/0
function to the add_two
module to test the restart semantics for a transient
child that terminates normally.
On my second attempt, I decided to start anew, but in the order 3-1-2. To answer the question "Why choose to identify the child through its Id
instead of the Pid
when stopping it", the child may be restarted so that the current Pid
will be unknown.