Skip to content

Commit 4ee90ff

Browse files
sodredumbbell
authored andcommitted
scripts/rabbitmq-server: Work around signal handling issue with Dash
On Debian-like distributions, `/bin/sh` defaults to `/bin/dash` which has a bug with signal handlers. In the case of Dash, it looks like `set -e` (set at the beginning of this script) gets precedence over signal handling. Therefore, when `wait` is interrupted, its exit code is non-zero and because of `set -e`, the script terminates immediately without running the signal handler. To work around this issue, we use `|| true` to force that statement to succeed and the signal handler to properly execute. Replace the use of `-e` on the shebang line by a standalone `set -e`, like other scripts. This way, the script behavior remains the same if the script is started as an argument to a shell. For instance: bash ./rabbitmq-server Bump the copyright year to 2017. Signed-off-by: Patrick Sodré <[email protected]> Fixes #1192.
1 parent 12d73ad commit 4ee90ff

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

scripts/rabbitmq-server

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh -e
1+
#!/bin/sh
22
## The contents of this file are subject to the Mozilla Public License
33
## Version 1.1 (the "License"); you may not use this file except in
44
## compliance with the License. You may obtain a copy of the License
@@ -12,9 +12,11 @@
1212
## The Original Code is RabbitMQ.
1313
##
1414
## The Initial Developer of the Original Code is GoPivotal, Inc.
15-
## Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved.
15+
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
1616
##
1717

18+
set -e
19+
1820
# Get default settings with user overrides for (RABBITMQ_)<var_name>
1921
# Non-empty defaults should be set in rabbitmq-env
2022
. `dirname $0`/rabbitmq-env
@@ -233,21 +235,38 @@ else
233235
# The Erlang VM should ignore SIGINT.
234236
RABBITMQ_SERVER_START_ARGS="${RABBITMQ_SERVER_START_ARGS} ${RABBITMQ_IGNORE_SIGINT_FLAG}"
235237

236-
# Signal handlers. They all stop RabbitMQ properly (using
237-
# rabbitmqctl stop). Depending on the signal, this script will exit
238-
# with a non-zero error code:
238+
# Signal handlers. They all stop RabbitMQ properly, using
239+
# rabbitmqctl stop. This script will exit with different exit codes:
239240
# SIGHUP SIGTERM SIGTSTP
240-
# They are considered a normal process termination, so the script
241-
# exits with 0.
241+
# Exits 0 since this is considered a normal process termination.
242242
# SIGINT
243-
# They are considered an abnormal process termination, the script
244-
# exits with the job exit code.
243+
# Exits 128 + $signal_number where $signal_number is 2 for SIGINT (see
244+
# http://pubs.opengroup.org/onlinepubs/009695399/utilities/kill.html).
245+
# This is considered an abnormal process termination. Normally, we
246+
# don't need to specify this exit code because the shell propagates it.
247+
# Unfortunately, the signal handler doesn't work as expected in Dash,
248+
# thus we need to explicitely restate the exit code.
245249
trap "stop_rabbitmq_server; exit 0" HUP TERM TSTP
246-
trap "stop_rabbitmq_server" INT
250+
trap "stop_rabbitmq_server; exit 130" INT
247251

248252
start_rabbitmq_server "$@" &
253+
rabbitmq_server_pid=$!
249254

250255
# Block until RabbitMQ exits or a signal is caught.
251256
# Waits for last command (which is start_rabbitmq_server)
252-
wait $!
257+
#
258+
# The "|| true" is here to work around an issue with Dash. Normally
259+
# in a Bourne shell, if `wait` is interrupted by a signal, the
260+
# signal handlers defined above are executed and the script
261+
# terminates with the exit code of `wait` (unless the signal handler
262+
# overrides that).
263+
# In the case of Dash, it looks like `set -e` (set at the beginning
264+
# of this script) gets precedence over signal handling. Therefore,
265+
# when `wait` is interrupted, its exit code is non-zero and because
266+
# of `set -e`, the script terminates immediately without running the
267+
# signal handler. To work around this issue, we use "|| true" to
268+
# force that statement to succeed and the signal handler to properly
269+
# execute. Because the statement below has an exit code of 0, the
270+
# signal handler has to restate the expected exit code.
271+
wait $rabbitmq_server_pid || true
253272
fi

0 commit comments

Comments
 (0)