Repository navigation
fix: keep container alive when pgrep misses the forked mongod - #11
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the CI failures where the "Start MongoDB" step aborts with
Error response from daemon: container ... is not running, whiledocker logsshows a completely clean, error-free MongoDB startup.Root cause:
PID=$(pgrep mongod)runs underset -euo pipefail. Whenpgrepmatches nothing it exits 1, which aborts the entire script — and sinceentrypoint.shis PID 1, the container goes down with it, killing the mongod that had just started successfully. Theif [ -z "$PID" ]guard instop_mongodwas never reachable for the same reason, and multiple matches brokeps -pthe same way.Change: a
mongod_pid()helper that cannot abort the script and never returns more than one PID, pluswait_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_mongodand both keep-alive loops).MONGO_LOG=STDOUTis the Dockerfile default, so the changed branch is the production path; thetail -fbranch for a custom log file is untouched.Test plan
Verified without Docker (no daemon available in the dev environment), via faked
mongod/mongosh/pgrepbinaries driving the real script:Unit tests against the real functions (8/8): empty
pgrepresult no longer aborts the script; multiple PIDs no longer breakps -p;stop_mongodnow actually reaches itsNo mongod process foundbranch. As a regression proof, the old line dies before its own-zcheck under identical conditions.End-to-end simulation of the whole script across four configurations (CI default, with password, with replica set, race case):
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 startupline, which surfaces in CI via thedocker logsdump added in MongoCamp/mongodb-github-action#2.