[erlang-questions] tcp connections dropped in gen_server

Reynaldo Baquerizo reynaldomic@REDACTED
Wed Sep 7 12:55:35 CEST 2011


> [I hope it's ok that I reply to the list - someone else might find
> this information useful as well. I am mentioning this only because
> you keep replying to me privately.]

Ooops, apologies ... I certainly didn't mean to write to you alone.

> It makes very little sense to restart these processes (to me), because
> the TCP connection will die as well. The external client can reconnect
> and start anew. Or am I missing something?

Indeed, the client will reconnect. But I think I found the problem. The
process that is listening for new connections in gen_tcp:accept/1 dies
at some point, all other processes with established connections are fine
but eventually crash (cause of bad input), no further reconnections
will be possible.

How can I isolate the listening process? or reestructure to restart it
if it crashes?

At the end, I simplified it and have this:

-module(comm_tcp).
-export([start_link/2, acceptor/2]).

start_link(SupPid, ListenSocket) ->
    {ok, proc_lib:spawn_link(?MODULE, acceptor, [SupPid, ListenSocket])}.

acceptor(SupPid, ListenSocket) ->
    {ok, Socket} = gen_tcp:accept(ListenSocket),
    {ok, Pid} = comm_client_sup:start_child(SupPid),
    %% gen_tcp:controlling_process(Socket, Pid),
    error_logger:info_msg("New connection from ~p~n", [Socket]),
    inet:setopts(Socket, [binary, {nodelay, true}, {active, true}]),
    loop(Socket).

loop(Socket) ->
    receive
	{tcp, Socket, Data} ->
	    error_logger:info_msg("Messaged received from ~p: ~p~n",[Socket, Data]),
            comm_lib:handle_message(Socket, Data),
	    loop(Socket);
	{tcp_closed, Socket} ->
	    error_logger:info_msg("Device at ~p disconnected~n",[Socket]); 
        _Any ->
	     loop(Socket)
    end.

--
Reynaldo




More information about the erlang-questions mailing list