From f5bd75ccedda65adf215e6bff7e33a7889d03d16 Mon Sep 17 00:00:00 2001 From: Mikhail Vazhnov Date: Thu, 26 Dec 2024 08:08:25 +0300 Subject: [PATCH] fix: Handle terminate signals in init script `tini` does not wait for child processes to close, so all processes will be killed immediately. This is why the container stops so fast. This fix makes the `init` script listen and handle terminate signals. It also ensures that child processes are shut down in reverse order with proper waiting for completion. --- docker/Dockerfile | 3 +-- docker/init_scripts/init | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 9c8ad5ff1..2ce593384 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -120,7 +120,6 @@ RUN apk add --no-cache \ mariadb-connector-c \ p7zip \ python3 \ - tini \ tzdata \ valkey @@ -152,5 +151,5 @@ VOLUME ["/romm/resources", "/romm/library", "/romm/assets", "/romm/config", "/re EXPOSE 8080 6379/tcp WORKDIR /romm -ENTRYPOINT ["/sbin/tini", "--", "/docker-entrypoint.sh"] +ENTRYPOINT ["/docker-entrypoint.sh"] CMD ["/init"] diff --git a/docker/init_scripts/init b/docker/init_scripts/init index 14a02105f..59859893d 100755 --- a/docker/init_scripts/init +++ b/docker/init_scripts/init @@ -115,19 +115,43 @@ watchdog_process_pid() { fi } +stop_process_pid() { + PROCESS=$1 + if [[ -f "/tmp/${PROCESS}.pid" ]]; then + PID=$(cat "/tmp/${PROCESS}.pid") || true + if [[ -d "/proc/${PID}" ]]; then + info_log "stopping ${PROCESS}" + kill "${PID}" || true + # wait for process exit + while [ -e "/proc/${PID}" ]; do sleep 0.1; done + fi + fi +} + +shutdown() { + # shutdown in reverse order + stop_process_pid scheduler + stop_process_pid worker + stop_process_pid watcher + stop_process_pid nginx + stop_process_pid gunicorn + stop_process_pid valkey-server +} + # switch to backend directory cd /backend || { error_log "/backend directory doesn't seem to exist"; } info_log "Starting up, please wait..." +# setup trap handler +exited=0 +trap 'exited=1 && shutdown' SIGINT SIGTERM EXIT + # clear any leftover PID files rm /tmp/*.pid -f # function definition done, lets start our main loop -while true; do - # check for died processes every 5 seconds - sleep 5 - +while ! ((exited)); do # Start Valkey server if we dont have a corresponding PID file # and REDIS_HOST is not set (which would mean we're using an external Redis/Valkey) if [[ -z ${REDIS_HOST:=""} ]]; then @@ -138,7 +162,7 @@ while true; do # but only if it was not successful since the last full docker container start if [[ ${ALEMBIC_SUCCESS:="false"} == "false" ]]; then if alembic upgrade head; then - debug_log "database schema migrations suceeded" + debug_log "database schema migrations succeeded" ALEMBIC_SUCCESS="true" else error_log "Something went horribly wrong with our database" @@ -165,4 +189,7 @@ while true; do watchdog_process_pid python worker # Start scheduler if we dont have a corresponding PID file watchdog_process_pid python scheduler + + # check for died processes every 5 seconds + sleep 5 done