title: Erlang: gen_server crashes when started over supervisor
body: <p>So, i spent LOTS of time and still didn't find the answer.</p>
  
  <p>I have simple tcp server in gen_server, which accepts messages via telnet and print them in console:</p>
  
  <pre><code>-module(st_server).
  
  -behaviour(gen_server).
  %% --------------------------------------------------------------------
  %% Include files
  %% --------------------------------------------------------------------
  
  %% --------------------------------------------------------------------
  %% External exports
  -export([start_link/0, start_link/1, stop/0]).
  
  %% gen_server callbacks
  -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  
  -define(SERVER, ?MODULE).
  -define(DEFAULT_PORT, 1055).
  
  -record(state, {lsock, port}).
  
  %% ====================================================================
  %% External functions
  %% ====================================================================
  
  %%--------------------------------------------------------------------
  %% @doc Starts the server.
  %%
  %% @spec start_link(Port::integer()) -&gt; {ok, Pid}
  %% where
  %%  Pid = pid()
  %% @end
  %%--------------------------------------------------------------------
  start_link(Port) -&gt;
      gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []),
      io:format("Server name: ~w, port: ~w.~n", [?SERVER, Port]).
  
  %% @spec start_link() -&gt; {ok, Pid}
  %% @doc Calls `start_link(Port)' using the default port.
  start_link() -&gt; start_link(?DEFAULT_PORT).
  
  %%-------------------------------------------------------------------- 
  %% @doc Stops the server. 
  %% @spec stop() -&gt; ok 
  %% @end 
  %%-------------------------------------------------------------------- 
  stop() -&gt;
      gen_server:cast(?SERVER, stop).
  
  %% ====================================================================
  %% Server functions
  %% ====================================================================
  
  %% --------------------------------------------------------------------
  %% Function: init/1
  %% Description: Initiates the server
  %% Returns: {ok, State}          |
  %%          {ok, State, Timeout} |
  %%          ignore               |
  %%          {stop, Reason}
  %% --------------------------------------------------------------------
  init([Port]) -&gt;
      {ok, LSock} = gen_tcp:listen(Port, [{active, true}]),
      {ok, #state{lsock = LSock, port = Port}, 0}.
  
  %% --------------------------------------------------------------------
  %% Function: handle_cast/2
  %% Description: Handling cast messages
  %% Returns: {noreply, State}          |
  %%          {noreply, State, Timeout} |
  %%          {stop, Reason, State}            (terminate/2 is called)
  %% --------------------------------------------------------------------
  handle_cast(stop, State) -&gt;
      {stop, normal, State}.
  
  handle_call(_Request, _From, State) -&gt;
      {reply, ok, State}.
  
  %% --------------------------------------------------------------------
  %% Function: handle_info/2
  %% Description: Handling all non call/cast messages
  %% Returns: {noreply, State}          |
  %%          {noreply, State, Timeout} |
  %%          {stop, Reason, State}            (terminate/2 is called)
  %% --------------------------------------------------------------------
  
  
  handle_info({tcp, _Socket, Data}, State) -&gt;
      io:format("Incoming info: ~s", [Data]),
      {noreply, State};
  
  handle_info({tcp_closed, _Socket}, #state{lsock = LSock} = State) -&gt;
      {ok, _Sock} = gen_tcp:accept(LSock),
      {noreply, State};
  
  handle_info(timeout, #state{lsock = LSock} = State) -&gt;
      {ok, _Sock} = gen_tcp:accept(LSock),
      {noreply, State}.
  
  %% --------------------------------------------------------------------
  %% Function: terminate/2
  %% Description: Shutdown the server
  %% Returns: any (ignored by gen_server)
  %% --------------------------------------------------------------------
  terminate(Reason, _State) -&gt;
      io:format("~nServer shutdown. Reason: ~s.~n", [Reason]),
      ok.
  
  %% --------------------------------------------------------------------
  %% Func: code_change/3
  %% Purpose: Convert process state when code is changed
  %% Returns: {ok, NewState}
  %% --------------------------------------------------------------------
  code_change(_OldVsn, _State, _Extra) -&gt;
      {ok, _State}.
  
  %% --------------------------------------------------------------------
  %%% Internal functions
  %% --------------------------------------------------------------------
  </code></pre>
  
  <p>And supervisor:</p>
  
  <pre><code>-module(st_sup).
  
  -behaviour(supervisor).
  %% --------------------------------------------------------------------
  %% Include files
  %% --------------------------------------------------------------------
  
  %% --------------------------------------------------------------------
  %% External exports
  %% --------------------------------------------------------------------
  -export([]).
  
  %% --------------------------------------------------------------------
  %% Internal exports
  %% --------------------------------------------------------------------
  -export([
       init/1,
       start_link/0
          ]).
  
  %% --------------------------------------------------------------------
  %% Macros
  %% --------------------------------------------------------------------
  -define(SERVER, ?MODULE).
  
  %% --------------------------------------------------------------------
  %% Records
  %% --------------------------------------------------------------------
  
  %% ====================================================================
  %% External functions
  %% ====================================================================
  
  start_link() -&gt;
      supervisor:start_link({local, ?SERVER}, ?MODULE, []).
  
  %% ====================================================================
  %% Server functions
  %% ====================================================================
  %% --------------------------------------------------------------------
  %% Func: init/1
  %% Returns: {ok,  {SupFlags,  [ChildSpec]}} |
  %%          ignore                          |
  %%          {error, Reason}
  %% --------------------------------------------------------------------
  init([]) -&gt;
      Server = {st_server, {st_server, start_link, []},
                  permanent, 2000, worker, [st_server]},
      Children = [Server],
      RestartStrategy = {one_for_one, 0, 1},
      {ok, {RestartStrategy, Children}}.
  
  %% ====================================================================
  %% Internal functions
  %% ====================================================================
  </code></pre>
  
  <p>I start this in erlang console:  </p>
  
  <pre><code>st_sup:start_link().
  </code></pre>
  
  <p>And get this:</p>
  
  <pre><code>Server name: st_server, port: 1055.
  ** exception exit: shutdown
  </code></pre>
  
  <p>This means, that supervisor shuts down server for some reason.
  If I start server by itself (st_server:start_link.) it works just fine.</p>
  
  <p>So, the question is how to make this work without shutdowns?</p>