Skip to content

Commit ec6b50f

Browse files
authored
fix(MongoDb): Wait for post-init startup readiness before replica set initiation (#1656)
1 parent 84f4f7c commit ec6b50f

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

src/Testcontainers.MongoDb/MongoDbBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ private static async Task InitiateReplicaSetAsync(MongoDbContainer container, Mo
198198
return;
199199
}
200200

201+
var readiness = new WaitIndicateReadiness(configuration);
202+
201203
// This is a simple workaround to use the default options, which can be configured
202204
// with custom configurations as needed.
203205
var options = new WaitStrategy();
@@ -212,6 +214,14 @@ private static async Task InitiateReplicaSetAsync(MongoDbContainer container, Mo
212214
return 0L.Equals(execResult.ExitCode);
213215
};
214216

217+
// The official MongoDB image starts mongod twice during the first-time
218+
// initialization (bootstrap + final process). Waiting here prevents the
219+
// replica set from being initiated while this handover is in progress,
220+
// which could otherwise cause the container start to fail:
221+
// https://github.com/testcontainers/testcontainers-dotnet/issues/1636.
222+
await WaitStrategy.WaitUntilAsync(() => readiness.UntilAsync(container), options.Interval, options.Timeout, options.Retries, ct)
223+
.ConfigureAwait(false);
224+
215225
await WaitStrategy.WaitUntilAsync(initiate, options.Interval, options.Timeout, options.Retries, ct)
216226
.ConfigureAwait(false);
217227
}

0 commit comments

Comments
 (0)