Skip to content

Instantly share code, notes, and snippets.

@zhongwencool
Last active September 11, 2016 10:39
Show Gist options
  • Save zhongwencool/708f8d9ba5ce9553a50a527257482ebb to your computer and use it in GitHub Desktop.
Save zhongwencool/708f8d9ba5ce9553a50a527257482ebb to your computer and use it in GitHub Desktop.
supervisor child_spec shutdown mechanism doesn't work when child process crash itself.
{application,zoo,
[{description,"Warm and lovely zoo"},
{vsn,"0.1.0"},
{registered,[]},
{mod,{zoo_app,[]}},
{applications,[kernel,stdlib, sasl]},
{env,[]},
{modules,[zoo_app,zoo_keeper,zoo_sup]}]}.
-module(zoo_app).
-behaviour(application).
-export([start/2 ,stop/1]).
start(_StartType, _StartArgs) ->
zoo_sup:start_link().
stop(_State) ->
ok.
-module(zoo_keeper).
-behaviour(gen_server).
-export([start_link/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
start_link(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [Name], []).
init([Name]) ->
erlang:process_flag(trap_exit, true),
io:format("~p [~-14.14s] is starting~n", [erlang:time(), Name]),
{ok, Name, 1000}.
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast(_Request, State) ->
{noreply, State}.
handle_info(timeout, State) ->
% must crash.
1=2,
{noreply, State};
handle_info(_Info, State) ->
{noreply, State}.
terminate(Reason, State) ->
io:format("terminate reason:~p~n", [Reason]),
io:format("~p [~14.14s] before terminate ~n", [erlang:time(), State]),
timer:sleep(2000),
io:format("~p [~14.14s] after terminate ~n", [erlang:time(), State]),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-module(zoo_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
Workers = [{keeper, {zoo_keeper, start_link, [keeper]}, permanent, brutal_kill, worker, [zoo_keeper]}],
SupFlags = {one_for_one, 1, 5},
{ok, {SupFlags, Workers}}.
@zhongwencool
Copy link
Author

erlc  *.erl&&erl
zoo_keeper.erl:24: Warning: no clause will ever match
Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Eshell V6.3  (abort with ^G)
1> application:ensure_all_started(zoo).

=PROGRESS REPORT==== 11-Sep-2016::12:14:43 ===
          supervisor: {local,sasl_safe_sup}
             started: [{pid,<0.39.0>},
                       {name,alarm_handler},
                       {mfargs,{alarm_handler,start_link,[]}},
                       {restart_type,permanent},
                       {shutdown,2000},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:14:43 ===
          supervisor: {local,sasl_safe_sup}
             started: [{pid,<0.40.0>},
                       {name,overload},
                       {mfargs,{overload,start_link,[]}},
                       {restart_type,permanent},
                       {shutdown,2000},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:14:43 ===
          supervisor: {local,sasl_sup}
             started: [{pid,<0.38.0>},
                       {name,sasl_safe_sup},
                       {mfargs,
                           {supervisor,start_link,
                               [{local,sasl_safe_sup},sasl,safe]}},
                       {restart_type,permanent},
                       {shutdown,infinity},
                       {child_type,supervisor}]

=PROGRESS REPORT==== 11-Sep-2016::12:14:43 ===
          supervisor: {local,sasl_sup}
             started: [{pid,<0.41.0>},
                       {name,release_handler},
                       {mfargs,{release_handler,start_link,[]}},
                       {restart_type,permanent},
                       {shutdown,2000},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:14:43 ===
         application: sasl
          started_at: nonode@nohost
{12,14,43} [keeper        ] is starting

=PROGRESS REPORT==== 11-Sep-2016::12:14:43 ===
          supervisor: {local,zoo_sup}
             started: [{pid,<0.47.0>},
                       {name,keeper},
                       {mfargs,{zoo_keeper,start_link,[keeper]}},
                       {restart_type,permanent},
                       {shutdown,brutal_kill},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:14:43 ===
         application: zoo
          started_at: nonode@nohost
{ok,[sasl,zoo]}
2> terminate reason:{{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
{12,14,44} [        keeper] before terminate
{12,14,46} [        keeper] after  terminate

=ERROR REPORT==== 11-Sep-2016::12:14:46 ===
** Generic server keeper terminating
** Last message in was timeout
** When Server state == keeper
** Reason for termination ==
** {{badmatch,2},
    [{zoo_keeper,handle_info,2,[{file,"zoo_keeper.erl"},{line,24}]},
     {gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,593}]},
     {gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,659}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,237}]}]}
{12,14,46} [keeper        ] is starting

=CRASH REPORT==== 11-Sep-2016::12:14:46 ===
  crasher:
    initial call: zoo_keeper:init/1
    pid: <0.47.0>
    registered_name: keeper
    exception exit: {{badmatch,2},
                     [{zoo_keeper,handle_info,2,
                                  [{file,"zoo_keeper.erl"},{line,24}]},
                      {gen_server,try_dispatch,4,
                                  [{file,"gen_server.erl"},{line,593}]},
                      {gen_server,handle_msg,5,
                                  [{file,"gen_server.erl"},{line,659}]},
                      {proc_lib,init_p_do_apply,3,
                                [{file,"proc_lib.erl"},{line,237}]}]}
      in function  gen_server:terminate/7 (gen_server.erl, line 804)
    ancestors: [zoo_sup,<0.45.0>]
    messages: []
    links: [<0.46.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 987
    stack_size: 27
    reductions: 218
  neighbours:

=SUPERVISOR REPORT==== 11-Sep-2016::12:14:46 ===
     Supervisor: {local,zoo_sup}
     Context:    child_terminated
     Reason:     {{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
     Offender:   [{pid,<0.47.0>},
                  {name,keeper},
                  {mfargs,{zoo_keeper,start_link,[keeper]}},
                  {restart_type,permanent},
                  {shutdown,brutal_kill},
                  {child_type,worker}]


=PROGRESS REPORT==== 11-Sep-2016::12:14:46 ===
          supervisor: {local,zoo_sup}
             started: [{pid,<0.49.0>},
                       {name,keeper},
                       {mfargs,{zoo_keeper,start_link,[keeper]}},
                       {restart_type,permanent},
                       {shutdown,brutal_kill},
                       {child_type,worker}]
terminate reason:{{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
{12,14,47} [        keeper] before terminate
{12,14,49} [        keeper] after  terminate

=ERROR REPORT==== 11-Sep-2016::12:14:49 ===
** Generic server keeper terminating
** Last message in was timeout
** When Server state == keeper
** Reason for termination ==
** {{badmatch,2},
    [{zoo_keeper,handle_info,2,[{file,"zoo_keeper.erl"},{line,24}]},
     {gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,593}]},
     {gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,659}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,237}]}]}

=CRASH REPORT==== 11-Sep-2016::12:14:49 ===
  crasher:
    initial call: zoo_keeper:init/1
    pid: <0.49.0>
    registered_name: keeper
    exception exit: {{badmatch,2},
                     [{zoo_keeper,handle_info,2,
                                  [{file,"zoo_keeper.erl"},{line,24}]},
                      {gen_server,try_dispatch,4,
                                  [{file,"gen_server.erl"},{line,593}]},
                      {gen_server,handle_msg,5,
                                  [{file,"gen_server.erl"},{line,659}]},
                      {proc_lib,init_p_do_apply,3,
                                [{file,"proc_lib.erl"},{line,237}]}]}
      in function  gen_server:terminate/7 (gen_server.erl, line 804)
    ancestors: [zoo_sup,<0.45.0>]
    messages: []
    links: [<0.46.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 610
    stack_size: 27
    reductions: 210
  neighbours:

=SUPERVISOR REPORT==== 11-Sep-2016::12:14:49 ===
     Supervisor: {local,zoo_sup}
     Context:    child_terminated
     Reason:     {{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
     Offender:   [{pid,<0.49.0>},
                  {name,keeper},
                  {mfargs,{zoo_keeper,start_link,[keeper]}},
                  {restart_type,permanent},
                  {shutdown,brutal_kill},
                  {child_type,worker}]


=SUPERVISOR REPORT==== 11-Sep-2016::12:14:49 ===
     Supervisor: {local,zoo_sup}
     Context:    shutdown
     Reason:     reached_max_restart_intensity
     Offender:   [{pid,<0.49.0>},
                  {name,keeper},
                  {mfargs,{zoo_keeper,start_link,[keeper]}},
                  {restart_type,permanent},
                  {shutdown,brutal_kill},
                  {child_type,worker}]


=INFO REPORT==== 11-Sep-2016::12:14:49 ===
    application: zoo
    exited: shutdown
    type: temporary

@zhongwencool
Copy link
Author

zhongwencool commented Sep 11, 2016

if I change zoo_sup.erl shutdown from brutal_kill to 1000ms,

Workers = [{keeper, {zoo_keeper, start_link, [keeper]}, permanent, brutal_kill, worker, [zoo_keeper]}],

to

Workers = [{keeper, {zoo_keeper, start_link, [keeper]}, permanent, 1000, worker, [zoo_keeper]}],

But I make zoo_keeper.erl terminate/2 runs 2000ms

terminate(Reason, State) ->
    io:format("terminate reason:~p~n", [Reason]),
    io:format("~p [~14.14s] before terminate ~n", [erlang:time(), State]),
    timer:sleep(2000),
    io:format("~p [~14.14s] after  terminate ~n", [erlang:time(), State]),
    ok.

The result is it print before and after terminate.
Here is the result.

erlc  *.erl&&erl
zoo_keeper.erl:24: Warning: no clause will ever match
Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Eshell V6.3  (abort with ^G)
1> application:ensure_all_started(zoo).

=PROGRESS REPORT==== 11-Sep-2016::12:28:39 ===
          supervisor: {local,sasl_safe_sup}
             started: [{pid,<0.39.0>},
                       {name,alarm_handler},
                       {mfargs,{alarm_handler,start_link,[]}},
                       {restart_type,permanent},
                       {shutdown,2000},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:28:39 ===
          supervisor: {local,sasl_safe_sup}
             started: [{pid,<0.40.0>},
                       {name,overload},
                       {mfargs,{overload,start_link,[]}},
                       {restart_type,permanent},
                       {shutdown,2000},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:28:39 ===
          supervisor: {local,sasl_sup}
             started: [{pid,<0.38.0>},
                       {name,sasl_safe_sup},
                       {mfargs,
                           {supervisor,start_link,
                               [{local,sasl_safe_sup},sasl,safe]}},
                       {restart_type,permanent},
                       {shutdown,infinity},
                       {child_type,supervisor}]

=PROGRESS REPORT==== 11-Sep-2016::12:28:39 ===
          supervisor: {local,sasl_sup}
             started: [{pid,<0.41.0>},
                       {name,release_handler},
                       {mfargs,{release_handler,start_link,[]}},
                       {restart_type,permanent},
                       {shutdown,2000},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:28:39 ===
         application: sasl
          started_at: nonode@nohost
{12,28,39} [keeper        ] is starting

=PROGRESS REPORT==== 11-Sep-2016::12:28:39 ===
          supervisor: {local,zoo_sup}
             started: [{pid,<0.47.0>},
                       {name,keeper},
                       {mfargs,{zoo_keeper,start_link,[keeper]}},
                       {restart_type,permanent},
                       {shutdown,1000},
                       {child_type,worker}]

=PROGRESS REPORT==== 11-Sep-2016::12:28:39 ===
         application: zoo
          started_at: nonode@nohost
{ok,[sasl,zoo]}
2> terminate reason:{{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
{12,28,40} [        keeper] before terminate
{12,28,42} [        keeper] after  terminate

=ERROR REPORT==== 11-Sep-2016::12:28:42 ===
** Generic server keeper terminating
** Last message in was timeout
** When Server state == keeper
** Reason for termination ==
** {{badmatch,2},
    [{zoo_keeper,handle_info,2,[{file,"zoo_keeper.erl"},{line,24}]},
     {gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,593}]},
     {gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,659}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,237}]}]}
{12,28,42} [keeper        ] is starting

=CRASH REPORT==== 11-Sep-2016::12:28:42 ===
  crasher:
    initial call: zoo_keeper:init/1
    pid: <0.47.0>
    registered_name: keeper
    exception exit: {{badmatch,2},
                     [{zoo_keeper,handle_info,2,
                                  [{file,"zoo_keeper.erl"},{line,24}]},
                      {gen_server,try_dispatch,4,
                                  [{file,"gen_server.erl"},{line,593}]},
                      {gen_server,handle_msg,5,
                                  [{file,"gen_server.erl"},{line,659}]},
                      {proc_lib,init_p_do_apply,3,
                                [{file,"proc_lib.erl"},{line,237}]}]}
      in function  gen_server:terminate/7 (gen_server.erl, line 804)
    ancestors: [zoo_sup,<0.45.0>]
    messages: []
    links: [<0.46.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 987
    stack_size: 27
    reductions: 218
  neighbours:

=SUPERVISOR REPORT==== 11-Sep-2016::12:28:43 ===
     Supervisor: {local,zoo_sup}
     Context:    child_terminated
     Reason:     {{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
     Offender:   [{pid,<0.47.0>},
                  {name,keeper},
                  {mfargs,{zoo_keeper,start_link,[keeper]}},
                  {restart_type,permanent},
                  {shutdown,1000},
                  {child_type,worker}]


=PROGRESS REPORT==== 11-Sep-2016::12:28:43 ===
          supervisor: {local,zoo_sup}
             started: [{pid,<0.49.0>},
                       {name,keeper},
                       {mfargs,{zoo_keeper,start_link,[keeper]}},
                       {restart_type,permanent},
                       {shutdown,1000},
                       {child_type,worker}]
terminate reason:{{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
{12,28,44} [        keeper] before terminate
{12,28,46} [        keeper] after  terminate

=ERROR REPORT==== 11-Sep-2016::12:28:46 ===
** Generic server keeper terminating
** Last message in was timeout
** When Server state == keeper
** Reason for termination ==
** {{badmatch,2},
    [{zoo_keeper,handle_info,2,[{file,"zoo_keeper.erl"},{line,24}]},
     {gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,593}]},
     {gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,659}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,237}]}]}

=CRASH REPORT==== 11-Sep-2016::12:28:46 ===
  crasher:
    initial call: zoo_keeper:init/1
    pid: <0.49.0>
    registered_name: keeper
    exception exit: {{badmatch,2},
                     [{zoo_keeper,handle_info,2,
                                  [{file,"zoo_keeper.erl"},{line,24}]},
                      {gen_server,try_dispatch,4,
                                  [{file,"gen_server.erl"},{line,593}]},
                      {gen_server,handle_msg,5,
                                  [{file,"gen_server.erl"},{line,659}]},
                      {proc_lib,init_p_do_apply,3,
                                [{file,"proc_lib.erl"},{line,237}]}]}
      in function  gen_server:terminate/7 (gen_server.erl, line 804)
    ancestors: [zoo_sup,<0.45.0>]
    messages: []
    links: [<0.46.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 610
    stack_size: 27
    reductions: 210
  neighbours:

=SUPERVISOR REPORT==== 11-Sep-2016::12:28:46 ===
     Supervisor: {local,zoo_sup}
     Context:    child_terminated
     Reason:     {{badmatch,2},
                  [{zoo_keeper,handle_info,2,
                               [{file,"zoo_keeper.erl"},{line,24}]},
                   {gen_server,try_dispatch,4,
                               [{file,"gen_server.erl"},{line,593}]},
                   {gen_server,handle_msg,5,
                               [{file,"gen_server.erl"},{line,659}]},
                   {proc_lib,init_p_do_apply,3,
                             [{file,"proc_lib.erl"},{line,237}]}]}
     Offender:   [{pid,<0.49.0>},
                  {name,keeper},
                  {mfargs,{zoo_keeper,start_link,[keeper]}},
                  {restart_type,permanent},
                  {shutdown,1000},
                  {child_type,worker}]


=SUPERVISOR REPORT==== 11-Sep-2016::12:28:46 ===
     Supervisor: {local,zoo_sup}
     Context:    shutdown
     Reason:     reached_max_restart_intensity
     Offender:   [{pid,<0.49.0>},
                  {name,keeper},
                  {mfargs,{zoo_keeper,start_link,[keeper]}},
                  {restart_type,permanent},
                  {shutdown,1000},
                  {child_type,worker}]


=INFO REPORT==== 11-Sep-2016::12:28:46 ===
    application: zoo
    exited: shutdown
    type: temporary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment