Erlang hangs over the supervisor: start_child

I want to start a supervisor with a process that will call more processes related to the supervisor. The program freezes when supervisor:start_child.

The supervisor launches the main child:

% supervisor (only part shown)

init([]) ->
    MainApp = ?CHILD_ARG(mainapp, worker, [self()]),
    {ok, { {one_for_one, 5, 10}, [MainApp]} }.

The main child begins here:

% mainapp (gen_server)

start_link([SuperPid]) when is_pid(SuperPid) ->
    io:format("Mainapp started~n"),
    gen_server:start_link({local, ?MODULE}, ?MODULE, [SuperPid], []).

init([SuperPid]) ->
    {ok, _Pid} = start_child(childapp, SuperPid),   % <-- here start the other
    {ok, #state{sup=SuperPid}}.

start_child(Module, SuperPid) ->                             % Module = childapp
    io:format("start child before~n"),                       % printed
    ChildSpec = ?CHILD(Module, worker),
    {ok, Pid} = supervisor:start_child(SuperPid, ChildSpec), % <-- here freezes
    io:format("start child after~n"),                        % not printed
    {ok, Pid}.

And another source source contains

% childapp

start_link([]) ->
    io:format("Child started~n"),
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%% gen_server interface

init([]) ->
    {ok, #state{}}.

What do I get when I launch the application:

erl -pa ebin -eval "application:start(mysuptest)"
Erlang R16B01 (erts-5.10.2) [source-bdf5300] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.2  (abort with ^G)
1> Mainapp started
start child before

and here it stops - it freezes and does not return to the erlang console, as usual. I do not receive errors or other messages. Any ideas? Am I starting a child right?

+4
source share
1 answer

, ( , gen_server, start_link init). gen_server . , mainapp. mainapp : start_child. , mainapp. .

, start_child mainapp init

, . , ( Pid) mainapp

init([SuperPid]) ->
    handle_cast(self(), {start, SuperPid}),   % <-- send a cast message to itself
    {ok, #state{sup=SuperPid}}.

. , mainapp .

+5

All Articles