Skip to content

Commit 9bd7d7a

Browse files
authored
fix: handle SIGTERM for graceful container shutdown (#1101)
## Summary - Add a `trap` handler for SIGTERM/SIGINT that forwards signals to all child processes - Replace foreground `sleep infinity` with backgrounded `sleep infinity & wait $!` so the trap can fire **Root cause**: bash as PID 1 has no default SIGTERM handler, and foreground `sleep infinity` blocks signal delivery. Docker sends SIGTERM on stop, nothing handles it, so the container hangs until Docker sends SIGKILL after the timeout. Closes #1083 ## Test plan - [ ] `docker run --rm -d --name lgtm grafana/otel-lgtm` then `docker stop lgtm` — should exit within a few seconds instead of hanging - [ ] Verify all services start normally (health checks pass) --------- Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 3b5cacd commit 9bd7d7a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

docker/logging.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ function run_with_logging() {
77
shift
88
envvar=$1
99
shift
10-
command=$*
1110
if [[ ${envvar} == "true" || ${ENABLE_LOGS_ALL:-false} == "true" ]]; then
1211
echo "Running ${name} logging=true"
13-
${command}
12+
exec "$@"
1413
else
1514
echo "Running ${name} logging=false"
16-
${command} >/dev/null 2>&1
15+
exec "$@" >/dev/null 2>&1
1716
fi
1817
}

docker/run-all.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
echo "Starting grafana/otel-lgtm ${LGTM_VERSION}"
44

5+
# Graceful shutdown: forward SIGTERM/SIGINT to all background jobs
6+
shutdown() {
7+
echo "Shutting down..."
8+
# Send SIGTERM to all background jobs (the wrapper scripts exec the
9+
# server process, so these PIDs are the actual server processes)
10+
jobs -p | xargs -r kill 2>/dev/null
11+
wait
12+
exit 0
13+
}
14+
trap shutdown SIGTERM SIGINT
15+
516
# Record global start time
617
start_time_global=$(date +%s)
718

@@ -156,4 +167,6 @@ echo " - 3000: Grafana (http://localhost:3000). User: admin, password: admin"
156167
echo " - 4040: Pyroscope endpoint"
157168
echo " - 9090: Prometheus endpoint"
158169

159-
sleep infinity
170+
# Wait for signal; backgrounded sleep allows the trap to fire
171+
sleep infinity &
172+
wait $!

0 commit comments

Comments
 (0)