[erlang-questions] X gen_server also died when monitored Y gen_server died. why?

Alin Popa alin.popa@REDACTED
Sun Jun 2 21:52:12 CEST 2019


Looks like you've linked the Y process to the X process, therefore when Y
exists it'll take down X as well.
What you could do is to make process X to trap exit:

```
process_flat(trap_exit, true)
```

Usually you could add that within the `init` function of process X.
When you do that, if any linked process exists, it'll send a message to the
process X instead of taking it down.

Hope that helps,
Alin

On Sun, Jun 2, 2019 at 7:01 PM I Gusti Ngurah Oka Prinarjaya <
okaprinarjaya@REDACTED> wrote:

> Hi,
>
> I create two gen_server. Let's say X gen_server, and Y gen_server.
>
> X gen_server do monitoring using erlang:monitor/2 to Y gen_server.
> When i do Y:stop(). X gen_server also died.
>
> I want to *X gen_server still alive* even monitored Y gen_server is died.
>
> This is X gen_server https://pastebin.com/G2xDNxMf
> This is Y gen_server https://pastebin.com/hWUfUPmn
>
> This is the result when i run my code
>
> 1> oka_oprek_agensrv:start_link().
>
> {ok,<0.86.0>}
>
> 2> oka_oprek_agensrv:start_bgensrv().
>
> {ok,start_bgensrv,
>
>     {<0.84.0>,#Ref<0.1555235413.310640643.180158>}}
>
> 3> oka_oprek_agensrv:hello().
>
> {ok,hello,{<0.84.0>,#Ref<0.1555235413.310640643.180166>}}
>
> 4> oka_oprek_bgensrv:hello().
>
> {ok,hello_from_b,
>
>     {<0.84.0>,#Ref<0.1555235413.310640643.180173>}}
>
> 5> oka_oprek_bgensrv:stop().
>
> Monitored process died. Ref=#Ref<0.1555235413.310640643.180161>,
> Pid=<0.88.0>
>
> ** exception exit: {normal,{gen_server,call,[bgensrv,stop]}}
>
>      in function  gen_server:call/2 (gen_server.erl, line 215)
>
> 6> oka_oprek_agensrv:hello().
>
> ** exception exit: {noproc,{gen_server,call,[agensrv,hello]}}
>
>      in function  gen_server:call/2 (gen_server.erl, line 215)
>
> 7>
>
>
> Please enlightenment
>
>
>
> Thank you
>
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190602/b0092f1b/attachment.htm>


More information about the erlang-questions mailing list