1
votes

I'm trying to connect a gen_server to another gen_server and during the connect the servers need to monitor each other and know when the server has crashed, either the entire node or the server process. after im doing the first start_link and one of the servers crashes the other server gets a message from the monitor in the code (handle_info function is activated), but when it happens for the second time the monitor sends the information directly to the shell (the message does not go through the handle_info and goes directly to the shell only visible using flush() inside the shell) and the server that was suppose to be alerted from the monitor doesn't receive any message. my code in the sending side:

handle_call({connect, Node, Who}, _From, _State) ->
  case Who of
    cdot -> ets:insert(address, {cdot, Node}), ets:insert(address, 
{Node, cdot}), monitor_node(Node, true);
    cact -> ets:insert(address, {cact, Node}), ets:insert(address, 
{Node, cdot}), monitor_node(Node ,true);
    ctitles -> ets:insert(address, {ctitles, Node}), 
ets:insert(address, {Node, cdot}), monitor_node(Node, true);
    _-> ok
  end,
  [{_, Pid2}] = ets:lookup(?name_table3, pidGui),
  Pid2 ! {db, "Node "++ atom_to_list(Who) ++ " connected"}, %print to 
gui witch node was connected
  {reply, {{node(), self()}, connected}, node()};

and the one in the receiving side is:

connect() ->
  {{Node, Pid}, Connected} = gen_server:call(server_node(), {connect, 
node(), cact}),
  monitor_node(Node, true),
  monitor(process, Pid),
  Connected.

please can anyone tell me why this is happening?

the same happens for either node or process monitoring

2

2 Answers

2
votes

If you get the second monitor message in the shell, it is because you call the connect function in the shell context.

Check how you call this function, it must be done in the server context, it means inside a handle_call, handle_cast or handle_info function.

0
votes

after im doing the first start_link and one of the servers crashes the other server gets a message from the monitor in the code, but when it happens for the second time

It sounds like you are starting a new server after a server crashes. Do you call monitor() on the new server Pid?

A monitor is triggered only once, after that it is removed from both monitoring process and the monitored entity. Monitors are fired when the monitored process or port terminates, does not exist at the moment of creation, or if the connection to it is lost. In the case with connection, we lose knowledge about the fact if it still exists or not. The monitoring is also turned off when demonitor/1 is called.