From cfa43a35591f1e7595c812cc848dbf406cbd684d Mon Sep 17 00:00:00 2001 From: QuadStingray Date: Sat, 12 Sep 2026 15:47:00 +0000 Subject: [PATCH] fix: keep container alive when pgrep misses the forked mongod The container died right after a successful MongoDB start, leaving CI with only "container ... is not running" and a docker log that showed a clean, error-free startup. Cause: `PID=$(pgrep mongod)` runs under `set -euo pipefail`. When pgrep finds nothing it exits 1, which aborts the whole script - and since entrypoint.sh is PID 1, the container goes down with it, taking the freshly started mongod along. The `if [ -z "$PID" ]` guard in stop_mongod was never reachable for the same reason. Multiple matches broke `ps -p` in the same way. Guard the lookup, retry briefly for the forked process to appear, and fail loudly instead of silently ending the script if mongod really is gone. --- entrypoint.sh | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index a161ac2..3ace50e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -47,11 +47,42 @@ setup_mongosh_home() { export XDG_CONFIG_HOME="${mongosh_runtime_home}/.config" } +mongod_pid() { + # pgrep exits 1 when nothing matches, which would abort the whole script via + # `set -e` before the caller can react. Also never return more than one pid. + pgrep mongod 2>/dev/null | head -n 1 || true +} + +wait_for_mongod_shutdown() { + local pid + local waited=0 + + # mongod --fork returns once the server accepts connections, but the forked + # process can need a moment before it shows up in the process table + pid=$(mongod_pid) + while [[ -z "${pid}" && ${waited} -lt 10 ]]; do + sleep 1 + waited=$((waited + 1)) + pid=$(mongod_pid) + done + + if [[ -z "${pid}" ]]; then + echo "[entrypoint.sh] ERROR: mongod is not running after startup" + exit 1 + fi + + echo "[entrypoint.sh] MongoDb running with PID ${pid}" + while ps -p "${pid}" &>/dev/null; do + sleep 10 + done + echo "[entrypoint.sh] mongod process ${pid} has exited" +} + stop_mongod() { local auth_args=() echo "[entrypoint.sh] Stop MongoDb" - PID=$(pgrep mongod) + PID=$(mongod_pid) if [ -z "$PID" ]; then echo "[entrypoint.sh] No mongod process found" return @@ -193,17 +224,11 @@ if [[ ${MONGO_LOG} != 'NONE' && ${MONGO_LOG} != '' ]]; then echo "[entrypoint.sh] Start following the mongodb log" tail -f "${MONGO_LOG}" else - PID=$(pgrep mongod) - while ps -p "${PID}" &>/dev/null; do - sleep 10 - done + wait_for_mongod_shutdown fi else echo "[entrypoint.sh] Starting mongod..." print_mongod_args mongod --port "${MONGO_PORT}" --dbpath "${MONGO_DATA_DIR}" "${mongo_extra_args[@]}" --syslog --fork 2>&1 - PID=$(pgrep mongod) - while ps -p "${PID}" &>/dev/null; do - sleep 10 - done + wait_for_mongod_shutdown fi