Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ on:
- 'scripts/tests/test_docker_metadata.py'
- '.github/workflows/docker.yml'
- 'DOCKER/Dockerfile'
- 'DOCKER/docker-entrypoint.sh'
- 'DOCKER/test-entrypoint.sh'
- 'scripts/test-local-gates.sh'
- 'Makefile'
- '.github/workflows/release.yml'
Expand All @@ -23,6 +25,8 @@ on:
- 'scripts/tests/test_docker_metadata.py'
- '.github/workflows/docker.yml'
- 'DOCKER/Dockerfile'
- 'DOCKER/docker-entrypoint.sh'
- 'DOCKER/test-entrypoint.sh'
- 'scripts/test-local-gates.sh'
- 'Makefile'
- '.github/workflows/release.yml'
Expand All @@ -39,6 +43,8 @@ jobs:
run: python3 -B -m unittest discover -s scripts/release -p test_release.py
- name: Check Docker publication contract
run: python3 -B -m unittest discover -s scripts/tests -p test_docker_metadata.py
- name: Check Docker entrypoint signal handling
run: bash DOCKER/test-entrypoint.sh
- name: Check lint base selection
run: sh scripts/test-local-gates.sh
- name: Check shell syntax
Expand Down
16 changes: 12 additions & 4 deletions DOCKER/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ got_signal=false
_forward_signal() {
echo "Caught signal! Forwarding to tenderdash process."
got_signal=true
interrupted=true
kill -s "$1" "$child"
}

Expand Down Expand Up @@ -52,12 +53,19 @@ fi
# Start tenderdash in the background
tenderdash "$@" 2>&1 &
child=$!
wait "$child"
exit_code=$?
# A trapped signal interrupts wait before tenderdash exits; wait again until it
# returns tenderdash's own status, so its graceful shutdown completes before
# this script (PID 1) exits.
while :; do
interrupted=false
wait "$child"
exit_code=$?
[ "$interrupted" = true ] || break
done
Comment thread
Claudius-Maginificent marked this conversation as resolved.
Resolved

if [ $got_signal == false ] && [ $exit_code -ne 0 ] && [ "$1" == "start" ]; then
if [ $got_signal == false ] && [ "$exit_code" -ne 0 ] && [ "$1" == "start" ]; then
echo "Sleeping for 10 seconds as workaround for the busy port problem. See entrypoint code for details."
sleep 10
fi

exit $exit_code
exit "$exit_code"
68 changes: 68 additions & 0 deletions DOCKER/test-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
# Checks how docker-entrypoint.sh forwards signals and reports tenderdash's exit
# status, using a fake tenderdash on PATH.
set -euo pipefail

entrypoint="$(cd "$(dirname "$0")" && pwd)/docker-entrypoint.sh"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
mkdir -p "$work/bin" "$work/home/config"

# fake_tenderdash BODY: the fake runs BODY, then idles in an interruptible wait,
# so its traps run as soon as a signal arrives.
fake_tenderdash() {
printf '#!/bin/bash\n%s\nwhile :; do sleep 60 & wait $!; done\n' "$1" >"$work/bin/tenderdash"
chmod +x "$work/bin/tenderdash"
}

# run_entrypoint ARG SIGNALS: starts the entrypoint, sends SIGTERM SIGNALS times
# (0.3 s apart), and prints its exit status.
run_entrypoint() {
local pid status=0
rm -f "$work/cleaned"
# A wedged run fails instead of hanging CI. --foreground keeps timeout from
# signalling the fake tenderdash itself; --preserve-status passes on the
# entrypoint's status after timeout forwards our SIGTERM to it.
PATH="$work/bin:$PATH" TMHOME="$work/home" MARKER="$work/cleaned" \
timeout --foreground --preserve-status -k 5 60 bash "$entrypoint" "$1" >/dev/null 2>&1 &
pid=$!
sleep 0.3
for ((i = 0; i < $2; i++)); do
kill -TERM "$pid"
sleep 0.3
done
wait "$pid" || status=$?
echo "$status"
}

fail() {
echo "FAIL: $*" >&2
exit 1
}

# Tenderdash finishes a slow shutdown before the entrypoint exits, and its
# exit status is kept.
# shellcheck disable=SC2016 # expanded by the fake tenderdash
fake_tenderdash 'trap "sleep 1; touch \"\$MARKER\"; exit 7" TERM'
status=$(run_entrypoint start 1)
[ -f "$work/cleaned" ] || fail "entrypoint exited before tenderdash finished its shutdown"
[ "$status" = 7 ] || fail "slow shutdown: want status 7, got $status"

# A second signal during the shutdown does not end the wait.
status=$(run_entrypoint start 2)
[ -f "$work/cleaned" ] || fail "second signal: entrypoint exited before tenderdash finished"
[ "$status" = 7 ] || fail "second signal: want status 7, got $status"

# Tenderdash exiting at once on the signal still reports its own status.
fake_tenderdash 'trap "exit 0" TERM'
for _ in $(seq 20); do
status=$(run_entrypoint start 1)
[ "$status" = 0 ] || fail "immediate exit after signal: want status 0, got $status"
done

# Without a signal, the entrypoint returns tenderdash's status.
printf '#!/bin/bash\nexit 3\n' >"$work/bin/tenderdash"
status=$(run_entrypoint version 0)
[ "$status" = 3 ] || fail "plain exit: want status 3, got $status"

echo "docker-entrypoint.sh: all checks passed"
Loading