Skip to content

fix: keep container alive when pgrep misses the forked mongod - #11

Merged
QuadStingray merged 1 commit into
mainfrom
fix/mongod-pid-container-exit
Sep 13, 2026
Merged

QuadStingray merged 1 commit into
mainfrom
fix/mongod-pid-container-exit

Conversation

@QuadStingray

@QuadStingray QuadStingray commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes the CI failures where the "Start MongoDB" step aborts with Error response from daemon: container ... is not running, while docker logs shows a completely clean, error-free MongoDB startup.

Root cause: PID=$(pgrep mongod) runs under set -euo pipefail. When pgrep matches nothing it exits 1, which aborts the entire script — and since entrypoint.sh is PID 1, the container goes down with it, killing the mongod that had just started successfully. The if [ -z "$PID" ] guard in stop_mongod was never reachable for the same reason, and multiple matches broke ps -p the same way.

Change: a mongod_pid() helper that cannot abort the script and never returns more than one PID, plus wait_for_mongod_shutdown() which briefly retries for the forked process to appear and — if mongod really is gone — fails loudly with an explicit error instead of silently ending the script. Replaces all three affected call sites (stop_mongod and both keep-alive loops).

MONGO_LOG=STDOUT is the Dockerfile default, so the changed branch is the production path; the tail -f branch for a custom log file is untouched.

Test plan

Verified without Docker (no daemon available in the dev environment), via faked mongod/mongosh/pgrep binaries driving the real script:

  • Unit tests against the real functions (8/8): empty pgrep result no longer aborts the script; multiple PIDs no longer break ps -p; stop_mongod now actually reaches its No mongod process found branch. As a regression proof, the old line dies before its own -z check under identical conditions.

  • End-to-end simulation of the whole script across four configurations (CI default, with password, with replica set, race case):

    before after
    Normal operation — container stays up ✅ ✅
    Container exits cleanly when mongod dies ✅ ✅
    Race (pgrep sees mongod with a delay) ❌ container dead ✅
  • Real CI run on this branch / next tag build.

Note: if some second cause is still hiding behind the same symptom, the container now writes an explicit ERROR: mongod is not running after startup line, which surfaces in CI via the docker logs dump added in MongoCamp/mongodb-github-action#2.

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.
@QuadStingray
QuadStingray merged commit 7081574 into main Sep 13, 2026
@QuadStingray
QuadStingray deleted the fix/mongod-pid-container-exit branch September 13, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant