Skip to content

Commit 278a1fe

Browse files
authored
Merge pull request #2244 from rabbitmq/only-handle-SIGHUP-and-SIGTSTP
Only handle SIGHUP and SIGTSTP
2 parents 1081e8e + 02aa73c commit 278a1fe

File tree

5 files changed

+97
-68
lines changed

5 files changed

+97
-68
lines changed

apps/rabbitmq_prelaunch/src/rabbit_prelaunch.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ do_run() ->
4848
%% Configure dbg if requested.
4949
rabbit_prelaunch_early_logging:enable_quick_dbg(rabbit_env:dbg_config()),
5050

51+
%% Setup signal handler.
52+
ok = rabbit_prelaunch_sighandler:setup(),
53+
5154
%% We assert Mnesia is stopped before we run the prelaunch
5255
%% phases.
5356
%%
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
-module(rabbit_prelaunch_sighandler).
2+
-behaviour(gen_event).
3+
4+
-export([setup/0,
5+
init/1,
6+
handle_event/2,
7+
handle_call/2,
8+
handle_info/2,
9+
terminate/2,
10+
code_change/3]).
11+
12+
%% CAUTION: Signal handling in this module must be kept consistent
13+
%% with the same handling in rabbitmq-server(8).
14+
15+
%% #{signal => default | ignore | stop}.
16+
-define(SIGNALS_HANDLED_BY_US,
17+
#{
18+
%% SIGHUP is often used to reload the configuration or reopen
19+
%% log files after they were rotated. We don't support any
20+
%% of those two cases, so ignore it for now, until we can do
21+
%% something about it.
22+
sighup => ignore,
23+
24+
%% SIGTSTP is triggered by Ctrl+Z to pause a program. However
25+
%% we can't handle SIGCONT, the signal used to resume the
26+
%% program. Unfortunately, it makes a SIGTSTP handler less
27+
%% useful here.
28+
sigtstp => ignore
29+
}).
30+
31+
-define(SIGNAL_HANDLED_BY_ERLANG(Signal),
32+
Signal =:= sigusr1 orelse
33+
Signal =:= sigquit orelse
34+
Signal =:= sigterm).
35+
36+
-define(SERVER, erl_signal_server).
37+
38+
setup() ->
39+
case whereis(?SERVER) of
40+
undefined ->
41+
ok;
42+
_ ->
43+
case lists:member(?MODULE, gen_event:which_handlers(?SERVER)) of
44+
true -> ok;
45+
false -> gen_event:add_handler(?SERVER, ?MODULE, [])
46+
end
47+
end.
48+
49+
init(_Args) ->
50+
maps:fold(
51+
fun
52+
(Signal, _, Ret) when ?SIGNAL_HANDLED_BY_ERLANG(Signal) -> Ret;
53+
(Signal, default, ok) -> os:set_signal(Signal, default);
54+
(Signal, ignore, ok) -> os:set_signal(Signal, ignore);
55+
(Signal, _, ok) -> os:set_signal(Signal, handle)
56+
end, ok, ?SIGNALS_HANDLED_BY_US),
57+
{ok, #{}}.
58+
59+
handle_event(Signal, State) when ?SIGNAL_HANDLED_BY_ERLANG(Signal) ->
60+
{ok, State};
61+
handle_event(Signal, State) ->
62+
case ?SIGNALS_HANDLED_BY_US of
63+
#{Signal := stop} ->
64+
error_logger:info_msg(
65+
"~s received - shutting down~n",
66+
[string:uppercase(atom_to_list(Signal))]),
67+
ok = init:stop();
68+
_ ->
69+
error_logger:info_msg(
70+
"~s received - unhandled signal~n",
71+
[string:uppercase(atom_to_list(Signal))])
72+
end,
73+
{ok, State}.
74+
75+
handle_info(_, State) ->
76+
{ok, State}.
77+
78+
handle_call(_, State) ->
79+
{ok, ok, State}.
80+
81+
code_change(_OldVsn, State, _Extra) ->
82+
{ok, State}.
83+
84+
terminate(_Args, _State) ->
85+
ok.

scripts/rabbitmq-server

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ else
141141

142142
# Signal handlers. They all stop RabbitMQ properly, using
143143
# rabbitmqctl stop. This script will exit with different exit codes:
144-
# SIGHUP SIGTERM SIGTSTP
144+
# SIGHUP, SIGTSTP + SIGCONT
145+
# Ignored until we implement a useful behavior.
146+
# SIGTERM
145147
# Exits 0 since this is considered a normal process termination.
146148
# SIGINT
147149
# Exits 128 + $signal_number where $signal_number is 2 for SIGINT (see
@@ -150,7 +152,12 @@ else
150152
# don't need to specify this exit code because the shell propagates it.
151153
# Unfortunately, the signal handler doesn't work as expected in Dash,
152154
# thus we need to explicitly restate the exit code.
153-
trap "stop_rabbitmq_server; exit 0" HUP TERM TSTP
155+
#
156+
# The behaviors below should remain consistent with the
157+
# equivalent signal handlers in the Erlang code
158+
# (see apps/rabbitmq_prelaunch/src/rabbit_prelaunch_sighandler.erl).
159+
trap '' HUP TSTP CONT
160+
trap "stop_rabbitmq_server; exit 0" TERM
154161
trap "stop_rabbitmq_server; exit 130" INT
155162

156163
start_rabbitmq_server "$@" &

src/rabbit.erl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,6 @@
240240
{requires, pre_flight}
241241
]}).
242242

243-
-rabbit_boot_step({os_signal_handler,
244-
[{description, "registers an OS signal handler"},
245-
{mfa, {rabbit_sup, start_restartable_child,
246-
[rabbit_os_signal_handler]}},
247-
{requires, pre_flight}]}).
248-
249243
-rabbit_boot_step({direct_client,
250244
[{description, "direct client"},
251245
{mfa, {rabbit_direct, boot, []}},

src/rabbit_os_signal_handler.erl

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)