Skip to content

fix(MongoDb): Wait for post-init startup readiness before replica set initiation#1656

Merged
HofmeisterAn merged 1 commit intodevelopfrom
bugfix/mongodb-wait-before-init-replset
Mar 8, 2026
Merged

fix(MongoDb): Wait for post-init startup readiness before replica set initiation#1656
HofmeisterAn merged 1 commit intodevelopfrom
bugfix/mongodb-wait-before-init-replset

Conversation

@HofmeisterAn
Copy link
Collaborator

@HofmeisterAn HofmeisterAn commented Mar 8, 2026

What does this PR do?

The PR changes how the MongoDB replica set is initiated. Instead of initiating it immediately, it now waits until the final process is running.

The official MongoDB image starts mongod twice during the first-time initialization (bootstrap + final process). Waiting prevents the replica set from being initiated while this handover is in progress, which could otherwise cause the container startup to fail (container exits).

Why is it important?

We occasionally saw MongoDB tests fail because the container exited during startup with:

{"msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}}

Related issues

Summary by CodeRabbit

  • Bug Fixes
    • Improved MongoDB replica set initialization reliability by adding a readiness check before starting the replica set setup, preventing potential startup conflicts.

@HofmeisterAn HofmeisterAn added the bug Something isn't working label Mar 8, 2026
@netlify
Copy link

netlify bot commented Mar 8, 2026

Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit 22708ed
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-dotnet/deploys/69ad638c043fd4000884bc92
😎 Deploy Preview https://deploy-preview-1656--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link

coderabbitai bot commented Mar 8, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: be976518-48f6-4559-8b6a-d3c0db154e25

📥 Commits

Reviewing files that changed from the base of the PR and between 84f4f7c and 22708ed.

📒 Files selected for processing (1)
  • src/Testcontainers.MongoDb/MongoDbBuilder.cs

Walkthrough

The change introduces a readiness wait mechanism in MongoDB container initialization that pauses before attempting to initiate the replica set, preventing the replica set initialization from executing while the container is still stabilizing during startup.

Changes

Cohort / File(s) Summary
MongoDB Replica Set Initialization
src/Testcontainers.MongoDb/MongoDbBuilder.cs
Adds WaitIndicateReadiness prior to replica set initiation to prevent race conditions where the MongoDB container is still processing startup operations, guarding against socket binding failures.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Suggested labels

test flakiness

Poem

🐰 A patient hop before the bind,
We wait for readiness to align,
No more racing through startup's race,
MongoDB now sets at steady pace!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: waiting for post-init startup readiness before replica set initiation, directly matching the code modification.
Description check ✅ Passed The description includes mandatory sections (What/Why) and recommended sections (Related issues), providing clear context about the change and its rationale.
Linked Issues check ✅ Passed The PR directly addresses the objectives of issue #1636 by implementing a readiness wait before replica set initiation to prevent socket binding failures during MongoDB startup handover.
Out of Scope Changes check ✅ Passed All changes are in scope: adding a readiness wait in MongoDbBuilder.cs directly addresses the startup handover race condition identified in issue #1636.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix/mongodb-wait-before-init-replset

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@HofmeisterAn HofmeisterAn merged commit ec6b50f into develop Mar 8, 2026
16 checks passed
@HofmeisterAn HofmeisterAn deleted the bugfix/mongodb-wait-before-init-replset branch March 8, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MongoDB stops while starting

1 participant