testcontainers/testcontainers-java · error · ReplicaSetInitializationException

A single node replica set was not initialized in a set…

Error message

A single node replica set was not initialized in a set timeout: %d attempts

What it means

After issuing rs.initiate, MongoDBContainer repeatedly polls (up to AWAIT_INIT_REPLICA_SET_ATTEMPTS) for a single-node replica set master. checkMongoNodeExitCodeAfterWaiting throws ReplicaSetInitializationException when the poll command keeps exiting non-zero and the master never appears within the attempt budget.

Solutions

  1. Retry the test run — transient slowness on loaded CI hosts is common
  2. Use a supported, recent official mongo image
  3. Allocate more CPU/memory to Docker (or the CI runner) to speed up replica set election
  4. Enable container log following to diagnose why the master never becomes primary

Example fix

// before
new MongoDBContainer("mongo:3.6") // old image, flaky election on busy CI
// after
new MongoDBContainer(DockerImageName.parse("mongo:6.0"));
// and ensure the runner has enough CPU; rerun if transient
Defensive patterns

Strategy: retry

Try / catch

try {
    mongo.start();
} catch (ReplicaSetInitializationException e) {
    if (e.getMessage().contains("was not initialized")) {
        mongo.stop();
        mongo.start(); // retry once; election delays are often transient
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: The wait-for-master eval command fails on every attempt: slow container host making all attempts time out, mongod crashing after start, or an image whose mongo shell rejects the wait command.

Common situations: Heavily loaded CI runners where replica set election is slow; restricted cgroup/CPU environments; incompatible mongo image versions; Docker storage/network issues inside the container.

Understand the failure class

Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.

Related errors


AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12). Data as JSON: /api/errors/d55ba0e6e4294542. Report an issue: GitHub.

Appendix: source

Thrown at modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java:169

            "(%s) " +
            "{ " +
            "if (attempt > %d) {quit(1);} " +
            "print('%s ' + attempt); sleep(100);  attempt++; " +
            " }",
            "db.runCommand( { isMaster: 1 } ).ismaster==false",
            AWAIT_INIT_REPLICA_SET_ATTEMPTS,
            "An attempt to await for a single node replica set initialization:"
        );
    }

    private void checkMongoNodeExitCodeAfterWaiting(final Container.ExecResult execResultWaitForMaster) {
        if (execResultWaitForMaster.getExitCode() != CONTAINER_EXIT_CODE_OK) {
            final String errorMessage = String.format(
                "A single node replica set was not initialized in a set timeout: %d attempts",
                AWAIT_INIT_REPLICA_SET_ATTEMPTS
            );
            log.error(errorMessage);
            throw new ReplicaSetInitializationException(errorMessage);
        }
    }

    @SneakyThrows(value = { IOException.class, InterruptedException.class })
    private void initReplicaSet(boolean reused) {
        if (reused && isReplicationSetAlreadyInitialized()) {
            log.debug("Replica set already initialized.");
        } else {
            log.debug("Initializing a single node node replica set...");
            final ExecResult execResultInitRs = execInContainer(buildMongoEvalCommand("rs.initiate();"));
            log.debug(execResultInitRs.getStdout());
            checkMongoNodeExitCode(execResultInitRs);

            log.debug(
                "Awaiting for a single node replica set initialization up to {} attempts",
                AWAIT_INIT_REPLICA_SET_ATTEMPTS
            );
            final ExecResult execResultWaitForMaster = execInContainer(buildMongoEvalCommand(buildMongoWaitCommand()));

View on GitHub (pinned to 8e549514e3)