gen_tcp:connect will close when using supervisor.

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Tue Nov 24 15:26:20 CET 2020


On Thu, Nov 19, 2020 at 12:27 AM George Hope <george@REDACTED> wrote:

> I have a simple gen_server which connects to a tcp port, when I run
> gen_server directly:
> erl -noshell -s ttest start
> it works fine, But when I run:
> erl -noshell -s tt_sup start_link
> the connection will be closed instantly, it will work if I unlink the
> supervisor.
> How could I run my simple gen_server with supervisor ?
>
>
In the normal setup, supervisors have parents, and those parents are linked
such that errors in one end of the supervisor tree propagates. Supervisors
trap exit, so they have a way to stop that propagation, based on their
configuration. My guess is that when you run your program, the Erlang VM
spawns a process to handle your `-s` command. This process exits, and that
will reap your supervisor as well.

The right way of solving this is to start using applications. An
application has a top-level supervisor which is then handled by the
application manager in the system, and thus not being shut down as long as
the VM is running. It's a bit of a curve-ball I'm throwing you, I'm afraid.
Applications are far more involved to set up (and it leads into creating
releases as well, which is a subject of its own. Rebar3/Relx makes it
easier, but do note it isn't a simple process). On the other hand, as your
toy program is growing, it is likely to benefit more and more from doing it
right, so even if the ball curves a lot, you might end up catching it.
There is a point where switching to the application structure would be more
beneficial. If you are experienced as an erlang developer, I'm guessing you
just create the app straight away, but if you are less experienced, people
tend to look for the solutions found in other languages, and it doesn't
work as well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20201124/446bbc93/attachment.htm>


More information about the erlang-questions mailing list