Can Simple_one_for_one be terminated only if a SHUTDOWN strategy is assigned to brutal_kill?

An observer is an OTP behavior.

init([]) -> RoomSpec = {mod_zytm_room, {mod_zytm_room, start_link, []}, transient, brutal_kill, worker, [mod_zytm_room]}, {ok, {{simple_one_for_one, 10, 10000}, [RoomSpec]}}. 

Above code will call the terminate child method.

But if I change brutal_kill to an integer timeout (for example, 6000), the terminate method was never called.

I see the explanation in the Erlang document:

The dynamically created child-processes of a simple one-for-one supervisor were not explicitly killed, regardless of the shutdown strategy, but are expected to stop when the supervisor (i.e., when the output from the parent process is received).

But I can’t fully understand. Is it said that exit(Pid, kill) can complete the child specification simple_one_for_one , but exit(Pid, shutdown) cannot?

==================================== update =============== ========================

mod_zytm_room_sup.erl

 -module(mod_zytm_room_sup). -behaviour(supervisor). -export([start_link/0, init/1, open_room/1, close_room/1]). start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) -> RoomSpec = {mod_zytm_room, {mod_zytm_room, start_link, []}, transient, brutal_kill, worker, [mod_zytm_room]}, {ok, {{simple_one_for_one, 10, 10000}, [RoomSpec]}}. open_room(RoomId) -> supervisor:start_child(?MODULE, [RoomId]). close_room(RoomPid) -> supervisor:terminate_child(?MODULE, RoomPid). 

mod_zytm_room.erl

 -module(mod_zytm_room). -behaviour(gen_server). -export([start_link/1]). -export([init/1, handle_cast/2, handle_info/2, handle_call/3, code_change/3, terminate/2]). start_link(RoomId) -> gen_server:start_link(?MODULE, [RoomId], []). init([RoomId]) -> {ok, []}. terminate(_, _) -> error_logger:info_msg("~p terminated:~p", [?MODULE, self()]), ok. ...other methods ommited. 

mod_zytm_sup.erl

 -module(mod_zytm_sup). -behaviour(gen_server). -export([start_link/0]). -export([init/1, handle_cast/2, handle_info/2, handle_call/3, code_change/3, terminate/2]). start_link() -> gen_server:start_link(?MODULE, [], []). init([]) -> {ok, []}. %% invoked by an erlang:send_after event. handle_info({'CLOSE_ROOM', RoomPid}, State) -> mod_zytm_room_sup:close_room(RoomPid), {noreply, State}. ...other methods ommited. 

Both mod_zytm_sup and mod_zytm_room_sup are part of the system control tree, mod_zytm_sup invoke mod_zytm_room_sup to create or close the mod_zytm_room process.

+4
source share
1 answer

Sorry, I have the wrong result.

To make it clear:

  • brutal_kill strategy to kill the child process immediately.
  • The terminate method will be called if the shutdown simple_one_for_one strategy is an integer timeout. The child must declare process_flag(trap_exit, true) in its init callback.

FYI Erlang Document Guide:

If gen_server is part of the observation tree and the supervisor is ordered to complete, this function will be called with Reason = Shutdown if the following conditions apply:

gen_server is configured to capture output, and the shutdown strategy defined in the specification of the child supervisor is an integer timeout value, not brutal_kill.

+4
source

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


All Articles