<div dir="ltr"><div>Hi,</div><div><br></div><div>I ran your program with a small difference and found some threads to pull on. I added process_flag(trap_exit, true), to ttest:init. Running `erl -noshell  -s ttest start` now gives different output, and the other side of the socket gets closed:</div><div><br></div><div>gen_server start id is <0.9.0><br>connect id is <0.78.0><br>init id is <0.78.0><br>gen_server id is <0.78.0><br>=ERROR REPORT==== 20-Nov-2020::08:46:30.930438 ===<br>** Generic server ttest terminating<br>** Last message in was {'EXIT',<0.9.0>,normal}<br>** When Server state == []<br>** Reason for termination ==<br>** {function_clause,[{ttest,terminate,<br>                            [normal,[]],<br>                            [{file,"ttest.erl"},{line,29}]},<br>                     {gen_server,try_terminate,3,<br>                                 [{file,"gen_server.erl"},{line,718}]},<br>                     {gen_server,terminate,10,<br>                                 [{file,"gen_server.erl"},{line,903}]},<br>                     {proc_lib,init_p_do_apply,3,<br>                               [{file,"proc_lib.erl"},{line,226}]}]}<br><br>=CRASH REPORT==== 20-Nov-2020::08:46:30.954039 ===<br>  crasher:<br>    initial call: ttest:init/1<br>    pid: <0.78.0><br>    registered_name: ttest<br>    exception error: no function clause matching ttest:terminate(normal,[]) (ttest.erl, line 29)<br>      in function  gen_server:try_terminate/3 (gen_server.erl, line 718)<br>      in call from gen_server:terminate/10 (gen_server.erl, line 903)<br>    ancestors: [<0.9.0>]<br>    message_queue_len: 0<br>    messages: []<br>    links: [#Port<0.6>]<br>    dictionary: []<br>    trap_exit: true<br>    status: running<br>    heap_size: 4185<br>    stack_size: 28<br>    reductions: 6011<br>  neighbours:</div><div><br></div><div>The reason why this particular error gets printed is somewhat accidental, but the key bit is "** Last message in was {'EXIT',<0.9.0>,normal}" - this suggests that the process that was starting your gen_server (<0.9.0>) has itself exited with the reason `normal`, presumably right after running the function specified by the `-s` flag.</div><div><br></div><div>There's two key things to note here:</div><div><br></div><div>* a supervisor itself is implemented as a gen_server that traps exits. That is, it will notice any exit signal sent to it (barring `kill` perhaps?), and react to it</div><div>* normally (i.e. in absence of trap_exit), when two processes are linked an one of them exits with the reason `normal`, the other one does _not_exit</div><div><br></div><div>The result of this is that the supervisor you're starting notices <0.9.0> exiting and  exits itself, whereas your gen_server does not. This also explains why starting the supervisor and unlinking it also seemingly fixes the problem - in that case the exit signal of <0.9.0> never reaches the supervisor as there's no link by the time it exits (which seems to be right after tt_sup:start_link/start_shell returns).</div><div><br></div><div>The solution I'd suggest is making a rudimentary application (<a href="https://erlang.org/doc/design_principles/applications.html">https://erlang.org/doc/design_principles/applications.html</a>) containing your module and supervisor, or simply continue with the `unlink` solution if you're just messing around with some non-serious code.</div><div><br></div><div>Hope that helps!</div><div><br></div><div>Karl<br></div><div> <br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 18 Nov 2020 at 23:27, George Hope <<a href="mailto:george@1w1g.com">george@1w1g.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u><div><div style="font-family:Verdana,Arial,Helvetica,sans-serif;font-size:10pt"><div>I have a simple gen_server which connects to a tcp port, when I run gen_server directly:<br></div><div>erl -noshell  -s ttest start<br></div><div>it works fine, But when I run:<br></div><div>erl -noshell -s tt_sup start_link <br></div><div>the connection will be closed instantly, it will work if I unlink the supervisor.<br></div><div>How could I run my simple gen_server with supervisor ?<br></div><div><br></div><div><br></div><div>-module(tt_sup).<br></div><div>-behaviour(supervisor).<br></div><div>-export([init/1, start_link/0, start_shell/0]).<br></div><div><br></div><div>start_link() -><br></div><div>    io:format("Supervisor started with PID~p~n", [self()]),<br></div><div>    {ok, Pid} = supervisor:start_link(tt_sup, []),<br></div><div>    io:format("Supervisor PID=~p~n", [Pid]),<br></div><div>    {ok, Pid}.<br></div><div><br></div><div>start_shell() -><br></div><div>    io:format("Supervisor started with PID~p~n", [self()]),<br></div><div>    {ok, Pid} = supervisor:start_link(tt_sup, []),<br></div><div>    io:format("Supervisor PID=~p~n", [Pid]),<br></div><div>    unlink(Pid),<br></div><div>    {ok, Pid}.<br></div><div><br></div><div>init(_Args) -><br></div><div>    SupFlags = #{strategy => one_for_one},<br></div><div>    Child = [#{id => ttest, start => {ttest, start, []},<br></div><div>               restart => permanent, shutdown => brutal_kill,<br></div><div>               type => worker, modules => [ttest]}],<br></div><div>    {ok, {SupFlags, Child}}.<br></div><div><br></div><div><br></div><div>-module(ttest).<br></div><div>-export([start/0,init/1,handle_cast/2,handle_call/3,handle_info/2,terminate/2,connect/1]).<br></div><div>-behaviour(gen_server).<br></div><div><br></div><div>start() -><br></div><div>       io:format("gen_server start id is ~p~n",[self()]),<br></div><div>       {ok,Pid} = gen_server:start_link({local,ttest},ttest,[],[]),<br></div><div>       io:format("gen_server id is ~p~n",[Pid]),<br></div><div>       connect(Pid),<br></div><div>       {ok,Pid}.<br></div><div><br></div><div>init(_Args) -><br></div><div>    io:format("init id is ~p~n",[self()]),<br></div><div>        {ok,[]}.       <br></div><div>handle_call({tcp,Socket,Bin},_From,States) -><br></div><div>        {reply,States,States}.<br></div><div>handle_info({tcp,Socket,Bin},States) -><br></div><div>        io:format("got info msg"),<br></div><div>        {noreply,States}.<br></div><div><br></div><div>handle_cast({send,Msg},States) -><br></div><div>        {reply,States}.<br></div><div><br></div><div>connect(I) -><br></div><div>    io:format("connect id is ~p~n",[self()]),<br></div><div>        {ok,Socket} =gen_tcp:connect("localhost",6000,[binary,{active,true}]),<br></div><div>        gen_tcp:controlling_process(Socket,I).<br></div><div><br></div><div>terminate(shutdown, State) -><br></div><div>    io:format("terminating"),<br></div><div>    ok.<br></div></div><br></div></blockquote></div>