Erlang: monitor and message "DOWN"

If you carefully study the gproc_tests.erl file of the gproc project. I found the following code. A goodbye message is sent before erlang: monitor / 2, I think the DOWN message will not be received. Is it correct? If so, you should switch the two lines, right?

t_simple_aggr_counter() -> ?assert(gproc:reg({c,l,c1}, 3) =:= true), ?assert(gproc:reg({a,l,c1}) =:= true), ?assert(gproc:get_value({a,l,c1}) =:= 3), P = self(), P1 = spawn_link(fun() -> gproc:reg({c,l,c1}, 5), P ! {self(), ok}, receive {P, goodbye} -> ok end end), receive {P1, ok} -> ok end, ?assert(gproc:get_value({a,l,c1}) =:= 8), ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7), ?assert(gproc:get_value({a,l,c1}) =:= 12), P1 ! {self(), goodbye}, %<<===========This line R = erlang:monitor(process, P1), %<<======This line receive {'DOWN', R, _, _, _} -> gproc:audit_process(P1) end, ?assert(gproc:get_value({a,l,c1}) =:= 7). 
+4
source share
2 answers

calling erlang: monitor / 2 will still generate a message {'DOWN', ...} to the calling process, even if the monitored process has already died.

eg:

 1> F = fun() -> io:format("finished.~n") end. #Fun<erl_eval.20.111823515> 2> Pid = spawn(F). finished. <0.45.0> 3> erlang:monitor(process, Pid). % process Pid has already exited. #Ref<0.0.0.76> 4> flush(). Shell got {'DOWN',#Ref<0.0.0.76>,process,<0.45.0>,noproc} ok 
+10
source

According to the documentation of erlang: monitor / 2 : the message "DOWN" will be sent to the monitoring process if the element dies, if the element does not exist , or if the connection is lost for the node in which the element is located.

+5
source

Source: https://habr.com/ru/post/1400026/


All Articles