Skip to content

Retry MongoDB replica-set configuration during member recovery - #20796

Closed
Ankit Jain (radical) with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-mongodb-replica-set-health-check
Closed

Ankit Jain (radical) with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-mongodb-replica-set-health-check

Conversation

Copilot AI commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

MongoDB replica sets can fail to start when a member changes replication state during setup:

MongoDB.Driver.MongoNodeIsRecoveringException:
Server returned node is recovering error
(code = 11602, codeName = "InterruptedDueToReplStateChange").

Recovery interruptions were not retried during initiation or reconfiguration, including the reconfiguration immediately after successful initiation.

Retry this response throughout configuration probing, initiation, and reconfiguration using the existing bounded loop. Re-probing after partial initialization preserves the persisted configuration.

Add server-failpoint scenarios for transient recovery, permanent errors, and retry exhaustion, checking membership IDs, transactions, and change streams after recovery.

Validation

Passed: 38 replica-set unit tests. This also compiled the new functional scenarios.

MSBUILDTERMINALLOGGER=false ./.dotnet/dotnet test --project tests/Aspire.Hosting.MongoDB.Tests/Aspire.Hosting.MongoDB.Tests.csproj --no-launch-profile /p:SkipNativeBuild=true -- --filter-class '*.AddMongoDBReplicaSetTests' --filter-not-trait 'quarantined=true' --filter-not-trait 'outerloop=true'

Blocked: container-backed validation remains required. The focused functional run failed at the DCP prerequisite because the local Docker daemon socket was missing, before reaching the scenarios. The remaining run was stopped; this is not a passing recovery test.

MSBUILDTERMINALLOGGER=false ./.dotnet/dotnet test --project tests/Aspire.Hosting.MongoDB.Tests/Aspire.Hosting.MongoDB.Tests.csproj --no-launch-profile /p:SkipNativeBuild=true -- --filter-method '*.VerifyReplicaSetConfigurationHandlesCommandErrors' --filter-method '*.VerifyMongoDBMultiNodeReplicaSetResource' --filter-method '*.VerifyMongoDBMultiNodeReplicaWithDataShouldWorkAcrossUsages' --filter-not-trait 'quarantined=true' --filter-not-trait 'outerloop=true'

Fixes #19980
Fixes #21133
Fixes #20478

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

Co-authored-by: radical <1472+radical@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix flaky MongoDbReplicaSetFunctionalTests verify multi-node health Retry MongoDB replica-set configuration during member recovery Oct 7, 2026
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 20796

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 20796"

@github-actions

This comment has been minimized.

@radical

Copy link
Copy Markdown
Member

Copilot the PR description should have the issue# that it is fixing. And consider if we need specific tests for the new behavior including negative ones.

Co-authored-by: radical <1472+radical@users.noreply.github.com>

Copilot AI commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Copilot the PR description should have the issue# that it is fixing. And consider if we need specific tests for the new behavior including negative ones.

The PR description already links the change to #19980 in its “Fixes #19980” suffix. I added focused coverage for the transient recovery code and negative cases for other codes; all 38 replica-set builder tests pass. The follow-up is commit acdc37d. The multi-node functional test’s latest local run stopped at a DCP API startup timeout before reaching MongoDB.

@github-actions

This comment has been minimized.

MongoDB can interrupt the reconfiguration immediately after replica-set
initiation with InterruptedDueToReplStateChange (code 11602). This escaped
retry handling and marked the replica set FailedToStart.

Retry recovery interruptions during configuration reads, initiation, and
reconfiguration through the existing bounded loop. Re-probe after partial
initialization to preserve the persisted configuration, and retain the
last recovery error for retry-exhaustion diagnostics.

Add server-failpoint scenarios for recovery, permanent errors, and retry
exhaustion, checking membership and replica-set operations after recovery.

Validation: 38 replica-set unit tests pass. Container-backed scenarios
compile but could not run because the local Docker daemon is unavailable.

Fixes #21133
Fixes #20478

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Tests selector

4 / 100 PR test projects · 2 PR jobs, from 3 changed files.

Selected PR test projects (4 / 100)

Aspire.Cli.EndToEnd.Tests, Aspire.Hosting.MongoDB.Tests, Aspire.Hosting.Radius.Tests, Aspire.Playground.Tests

Selected PR jobs (2)

polyglot, typescript-api-compat


How these were chosen — grouped by what changed

🔧 src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs (changed source)
→ 1 directly: Aspire.Hosting.MongoDB.Tests
→ 2 via the project graph: Aspire.Hosting.Radius.Tests, Aspire.Playground.Tests (2 hops)

📦 affected project Aspire.Hosting.MongoDB
→ 1 test: Aspire.Cli.EndToEnd.Tests

🧪 tests/Aspire.Hosting.MongoDB.Tests/ReplicaSet/AddMongoDBReplicaSetTests.cs (changed test)
→ 1 directly: Aspire.Hosting.MongoDB.Tests

🧪 tests/Aspire.Hosting.MongoDB.Tests/ReplicaSet/MongoDbReplicaSetFunctionalTests.cs (changed test)
→ 1 directly: Aspire.Hosting.MongoDB.Tests

Job reasons

Job Triggered by
polyglot affected project Aspire.Hosting.MongoDB
typescript-api-compat affected project Aspire.Hosting.MongoDB

Selection computed for commit 35f2509.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking problems found. There is one low-severity test gap, posted inline.

PR description: it still says "Blocked: container-backed validation remains required", but all seven new VerifyReplicaSetConfigurationHandlesCommandErrors scenarios passed on Ubuntu in CI run 39988871153, against MongoDB 9.0.4. Please update it before merge.

Could not verify:

  • Recovery was only shown with injected fail points. All the logged failures predate this fix, and the 11 other replica-set tests in that CI run showed no retry from this race, so closing the linked issues assumes the flake stays gone on later runs.
  • Nothing was run locally. Only the Ubuntu test results were inspected, so it is unknown whether the container scenarios ran in the passing Windows job.

{
// A member can interrupt config reads while transitioning during initialization. Its persisted
// configuration is unknown, so retry before deciding whether the set needs to be initialized.
allMembersUninitialized = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has no test protecting it. The replSetGetConfig / 11602 scenario in VerifyReplicaSetConfigurationHandlesCommandErrors only runs against a fresh set. If this assignment were removed, the failure would be swallowed and replSetInitiate would still succeed on mongo1, with the same failure count and member IDs, so the test would still pass. (Worked out from the code, not run.)

The case it guards: a persisted member returns 11602 while a newly prepended empty member returns NotYetInitialized. Without this line, Aspire runs replSetInitiate on the empty member, creating a new set instead of extending the persisted one. The current code handles this correctly; nothing stops it regressing.

Suggested fix: add a case based on VerifyMongoDBMultiNodeReplicaWithDataShouldWorkAcrossUsages(MemberPrepended) that sets the fail point on the persisted member, then asserts that its data and member IDs survive.

@radical

Copy link
Copy Markdown
Member

[automated] Closing as superseded for the reported replica-set startup race by #21594, which waits for the initial member to become writable primary before reconfiguration. This PR’s broader recovery retries and failpoint tests were not incorporated; closing this PR does not mark its linked issues resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment