Erlang Supervisor Behavior Gotcha – Worker Init Params

23 May

When starting up a behavior like supervisor, the start_link func looks something like:

start_link() ->
  supervisor:start_link({local, ?MODULE}, ?MODULE, []).

Which in turn calls the initialization callback:

init(_Args) ->
  %% do some stuff.

Notice that the initialization callback takes 1 argument event though the params list (the third parameter) is empty in your start_link func.

So, naturally (am I alone in this?) I assumed that when defining a Child Spec for a Supervisor, the {M, F, A} definition would follow the same pattern:

Childspec = {child1,
            {child1, start, []},
            permanent, brutal_kill, worker, dynamic},
%% ...

But the above definition does not call child1:start/1, where the one parameter is the emtpy list. Instead, the list items are extracted as individual parameters. So {child1, start, [p1, p2, p3]} would call child1:start/3, and {child1, start, []} calls child1:start/0. Just something to look out for if you’re new to erlang and the supervisor behavior like me.

blog comments powered by Disqus