[erlang-questions] gen_server & trap_exit

Ulf Wiger ulf.wiger@REDACTED
Tue Apr 12 09:27:31 CEST 2011

Hi Roberto,

You are correct, and you're definitely not the first to be stumped by this.

Gen_server handles exits from the parent in a special way, assuming that the parent is a supervisor. If the server does *not* trap exits, obviously it doesn't matter, but even when trapping exits, the gen_server will unconditionally exit if it gets an exit from the parent. 

In the special case of starting gen_servers from the shell, the shell process is of course more likely to crash than the server itself, and that's exactly what's happening here.

If the module supports a start() function without linking, you might want to use that, or calling unlink/1 after starting the server.

Ulf W

On 9 Apr 2011, at 14:27, Roberto Ostinelli wrote:

> Dear list,
> I start a gen_server and in the initialization i set process_flag(trap_exit, true), obviously to allow the gen_server to catch child processes crashes.
> Whenever i start this gen_server from the shell and then issue an error in the shell, the gen_server exits too:
> 12>abc().
> ** exception error: undefined shell command abc/0
> =ERROR REPORT==== 9-Apr-2011::14:16:51 ===
> ** Generic server testserver terminating 
> ** Last message in was {'EXIT',<0.272.0>,
>                                {{shell_undef,abc,0},
>                                 [{shell,shell_undef,2},
>                                  {erl_eval,local_func,5},
>                                  {shell,exprs,7},
>                                  {shell,eval_exprs,7},
>                                  {shell,eval_loop,3}]}}
> I believe that this behavior of the server exiting because its parent exits is supposed to happen, since trap_exit doesn't let the gen_server trap its parent process exiting. What I believe happens is that the shell 'crashes' and so does the child process 'testserver'.
> Am I correct in my assumptions, or am I missing a fundamental point here?
> Thank you,
> r.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

Ulf Wiger, CTO, Erlang Solutions, Ltd.

More information about the erlang-questions mailing list