Crash on gen_server:start_link/3

Dinislam Salikhov Dinislam.Salikhov@REDACTED
Fri Apr 30 12:59:20 CEST 2021


Hello,

I have a gen_server that may fail in init/1. It returns {stop, Error} in this case.
According to documentation, I should get {error, Error} as a result of gen_server:start_link/3, but instead of that the calling process crashes with Error reason.

If I make trap_exit before gen_server:start_link() then I get the expected {error, Error} but there's a message about exited gen_server process in my mailbox.
But I can't distinguish it from other similar messages as I don't have a pid of the failed gen_server.

Even more interesting behavior is in shell.
# cat my_gen_server.erl
-module(my_gen_server).
-export([init/1]).
init(Arg) -> {stop, Arg}.
# erl 
1> self().
<0.79.0>
2> Q = gen_server:start_link(my_gen_server, 42, []).
{error,42}
=CRASH REPORT==== 30-Apr-2021::13:09:41.507086 ===
...
** exception error: 42
3> Q.
{error,42}
4> self().
<0.84.0>

The process crashes, but the shell keeps the return value intact.

So, it looks like either docs or implementation has a bug. What's the intended behavior?
By now, there is a workaround way to start linked gen_server without a crash:
spawn_link a proxy process which will trap_exit and do gen_server:start_link() and then provide the result in the outer process. Is there less ugly way?

P.S. I tried it in OTP-23.2.7.2, erts-11.1.8

Dinislam Salikhov


More information about the erlang-questions mailing list