Skip to content

Commit

Permalink
Make supervision tree fetching more error proof
Browse files Browse the repository at this point in the history
If a child is wrongly specified as `supervisor`, but it does not
implement this behaviour, the `supervisor:which_children/0` call can
return unpredictable values. (Especially if the worker is a `gen_server`
which can return any sort of funny value in its `handle_call`).

This issue has been seen in more than one open-source library.

Hopefully closes erlanglab#37
  • Loading branch information
gomoripeti committed Jun 1, 2017
1 parent 8ec15d2 commit da001b7
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions apps/epl_st/src/epl_st.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,32 @@ code_change(_OldVsn, State, _Extra) ->
%%% Internal functions
%%%===================================================================

generate_sup_tree({_Name, Pid, worker, _Mods}) ->
generate_sup_tree(Pid, worker) ->
worker_node(Pid);
generate_sup_tree({_Name, Pid, supervisor, _Mods}) ->
generate_sup_tree(Pid, supervisor) ->
Children = command(fun supervisor:which_children/1, [Pid]),
sup_node(Pid, generate_children(Children));
sup_node(Pid, generate_children(Children)).

generate_sup_tree(undefined) ->
#{};
generate_sup_tree(MasterPid) ->
{SupPid, _SupName} = command(fun application_master:get_child/1, [MasterPid]),
Children = command(fun supervisor:which_children/1, [SupPid]),
sup_node(SupPid, generate_children(Children)).

generate_children(Children) ->
generate_children(Children) when is_list(Children) ->
lists:filtermap(
fun({_, undefined, _, _}) ->
false;
(Child) ->
{true, generate_sup_tree(Child)}
({_, Pid, Type, _})
when is_pid(Pid), (Type =:= worker orelse Type =:= supervisor) ->
{true, generate_sup_tree(Pid, Type)};
(_Unexpected) ->
false
end,
Children).
Children);
generate_children(_Unexpected) ->
[].

worker_node(Pid) ->
tree_node(Pid, worker, []).
Expand Down

0 comments on commit da001b7

Please sign in to comment.