Tim
Tim

Reputation: 7464

Linked gen_server does not terminate

I have some server

-module(trivial).
-behaviour(gen_server).
-export([
    init/1,
    handle_call/3,
    handle_cast/2
]).

init([]) ->
    {ok, []}.

handle_call(_Msg, _From, State) ->
    {reply, ok, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

and then I start it from inside of other process that (instantly) terminates:

c(trivial).

Before = length([ Proc || Proc <- processes(), is_process_alive(Proc) ]).

Pid = spawn(fun() -> gen_server:start_link(trivial, [], []) end).

false = is_process_alive(Pid).

Before = length([ Proc || Proc <- processes(), is_process_alive(Proc) ]).

Unfortunately, the last assertion does not hold: there is +1 alive process. I would assume that gen_server:start_link inside of spawn created a link, so if the spawned process dies, the server would die as well. Why doesn't it happen? How would I make it happen?

Upvotes: 0

Views: 46

Answers (1)

Jos&#233; M
Jos&#233; M

Reputation: 3509

That happens because Pid has no more instructions to run, so it exits with the reason normal, which in this case is silently dropped. You have all the cases in the doc

Upvotes: 2

Related Questions