Albert Netymk
Albert Netymk

Reputation: 1112

erlang otp gen_server drops connection when handle_call returns noreply

Start the server using:

erlc server.erl ; erl -eval 'server:start()'

In another terminal:

telnet localhost 3547

Which could establish the connection successfully, but in a few seconds, the connection is closed by the server due to reasons beyond me. Reading the doc for handle_call/3, {noreply, NewState} is allowed as well.

Could someone explain it? Feels super confusing to me.

SOURCE CODE

-module(server).
-mode(compile).
-behavior(gen_server).

-compile(export_all).

-export([   main/1
          , start/0
          , stop/0
          , stop_and_wait/0
        ]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
        terminate/2, code_change/3]).

-define(SERVER, ?MODULE).
-define(DEFAULT_PORT, 3547).

-record(state, {port, lsock}).

start_link(Port) ->
  gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).

start() ->
  start_link(?DEFAULT_PORT).

stop() ->
  gen_server:cast(?SERVER, stop).

stop_and_wait() ->
  gen_server:call(?SERVER, stop, infinity).

init([Port]) ->
  {ok, LSock} = gen_tcp:listen(Port, [{active, false}, {reuseaddr, true}]),
  {ok, #state{port = Port, lsock = LSock}, 0}.

handle_call({do_stuff, Arg}, _From, State) ->
  io:format("do_stuff is called with ~p~n", [Arg]),
  % {reply, ok, State};
  {noreply, State};

handle_call(stop, _From, State) ->
  {stop, normal, ok, State}.

handle_cast(stop, State) ->
  {stop, normal, State}.

handle_info(timeout, #state{lsock = LSock} = State) ->
  Server = self(),
  Listener = spawn(fun() -> listen(Server, LSock) end),
  {noreply, State}.

terminate(_Reason, _State) ->
  ok.

code_change(_Oldvsn, State, _Extra) ->
  {ok, State}.

listen(Server, LSock) ->
  {ok, Socket} = gen_tcp:accept(LSock),
  gen_server:call(?SERVER, {do_stuff, 1}),
  listen(Server, LSock).

main(_) ->
  io:format("~p~n", [ok]),
  ok.

Upvotes: 2

Views: 279

Answers (1)

Steve Vinoski
Steve Vinoski

Reputation: 20004

Returning {noreply, NewState} from a gen_server:handle_call/3 implementation is allowed, but it does not mean the gen_server does not have to reply to the call. Rather, in that case it's assumed that the gen_server will reply at some later point using the gen_server:reply/2 call.

The default timeout for gen_server:call/2,3 is 5 seconds. What's happening in your code is your listen/2 function is running in a process that accepts a socket, and is thus the owner of that socket, after which it calls gen_server:call(?SERVER, {do_stuff, 1}). Since your gen_server does not reply to that call, the gen_server:call times out after 5 seconds, killing the process and thus closing the socket.

Upvotes: 9

Related Questions