1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
-module(deadlock).
-behaviour(application).
-compile(export_all).
-define(SUP,deadlock_sup).
-define(CHILD,deadlock_child).
%%%-----------------------------------------------------------------
%%% application callbacks
start(_StartType, _StartArgs) ->
supervisor:start_link({local, ?SUP}, ?MODULE, [sup]).
stop(_State) ->
ok.
%%%-----------------------------------------------------------------
%%% supervisor callbacks
init([sup]) ->
{ok, {{one_for_one, 5, 10}, [
{
sasl_syslog_dm, {?MODULE, start_link, []},
permanent, 25000, worker,
[deadlock]
}
]}};
%%%-----------------------------------------------------------------
%%% gen_server callbacks
init([child]) ->
case application:get_env(deadlock, fail_start) of
{ok, false} ->
process_flag(trap_exit, true),
io:format("~p: Traps exit~n",[?MODULE]),
%% we must not fail on the first init, otherwise supervisor
%% terminates immediately
{ok, []};
{ok, true} ->
timer:sleep(infinity), % init hangs!!!!
{ok, []}
end.
handle_call(_Req, _From, State) ->
{reply, ok, State}.
handle_cast(restart, State) ->
{stop, error, State}.
handle_info(_Msg, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
case application:get_env(deadlock, fail_stop) of
{ok, false} -> ok;
{ok, Tester} ->
Tester ! {deadlock, self()},
io:format("~p: Waiting in terminate (~p)~n",[?MODULE,Tester]),
receive continue -> ok end
end,
io:format("~p: terminates~n", [?MODULE]),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%-----------------------------------------------------------------
%%% Start child
start_link() ->
gen_server:start_link({local, ?CHILD}, ?MODULE, [child], []).
%%%-----------------------------------------------------------------
%%% Provoke hanging
restart_and_fail() ->
application:set_env(deadlock, fail_start, true), % next init will hang
gen_server:cast(?CHILD, restart).
|