spawn - Erlang: Why can't I link two gen_servers? -
i have 2 gen_server modules. 
 first serv.erl
-module(serv). -behaviour(gen_server). -export([init/1,       handle_call/3,      handle_cast/2,       handle_info/2,      code_change/3,      terminate/2,      start_link/0     ]). start_link() ->     gen_server:start_link(?module, [], []).  init([]) ->     process_flag(trap_exit, true),     spawn_link(user, start_link,[]),     {ok, []}.  handle_call(_e, _from, state) ->         {noreply, state}.  handle_cast(_message, state) ->     {noreply, state}.  terminate(_reason, _state) ->     ok.  handle_info(message, state) ->     {noreply, state}.  code_change(_oldversion, state, _extra) ->     {ok, state}. and user.erl (which same except init/1):
init([]) ->      {ok, []}. i thought servers last forever. , if first server dies 1 gets {'exit', pid, reason} message.
but if start modules serv:start_link() , user module exit after start message {'exit',pid,normal} . why user die?
when use spawn link function, starting new process calls user:start_link.  process starts , links user gen_server process, , exits, since call user:start_link returned.  user process linked process, gets exit signal.  since user process isn't trapping exits, exits.
you should run user:start_link in serv:init function, suggested in comments.
Comments
Post a Comment