{"record":{"id":"d55ba0e6e4294542","repo":"testcontainers/testcontainers-java","slug":"a-single-node-replica-set-was-not-initialized-in-a-set","errorCode":null,"errorMessage":"A single node replica set was not initialized in a set timeout: %d attempts","messagePattern":"A single node replica set was not initialized in a set timeout: (.+?) attempts","errorType":"exception","errorClass":"ReplicaSetInitializationException","httpStatus":null,"severity":"error","filePath":"modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java","lineNumber":169,"sourceCode":"            \"(%s) \" +\n            \"{ \" +\n            \"if (attempt > %d) {quit(1);} \" +\n            \"print('%s ' + attempt); sleep(100);  attempt++; \" +\n            \" }\",\n            \"db.runCommand( { isMaster: 1 } ).ismaster==false\",\n            AWAIT_INIT_REPLICA_SET_ATTEMPTS,\n            \"An attempt to await for a single node replica set initialization:\"\n        );\n    }\n\n    private void checkMongoNodeExitCodeAfterWaiting(final Container.ExecResult execResultWaitForMaster) {\n        if (execResultWaitForMaster.getExitCode() != CONTAINER_EXIT_CODE_OK) {\n            final String errorMessage = String.format(\n                \"A single node replica set was not initialized in a set timeout: %d attempts\",\n                AWAIT_INIT_REPLICA_SET_ATTEMPTS\n            );\n            log.error(errorMessage);\n            throw new ReplicaSetInitializationException(errorMessage);\n        }\n    }\n\n    @SneakyThrows(value = { IOException.class, InterruptedException.class })\n    private void initReplicaSet(boolean reused) {\n        if (reused && isReplicationSetAlreadyInitialized()) {\n            log.debug(\"Replica set already initialized.\");\n        } else {\n            log.debug(\"Initializing a single node node replica set...\");\n            final ExecResult execResultInitRs = execInContainer(buildMongoEvalCommand(\"rs.initiate();\"));\n            log.debug(execResultInitRs.getStdout());\n            checkMongoNodeExitCode(execResultInitRs);\n\n            log.debug(\n                \"Awaiting for a single node replica set initialization up to {} attempts\",\n                AWAIT_INIT_REPLICA_SET_ATTEMPTS\n            );\n            final ExecResult execResultWaitForMaster = execInContainer(buildMongoEvalCommand(buildMongoWaitCommand()));","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java#L151-L187","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Retry the test run — transient slowness on loaded CI hosts is common","Use a supported, recent official mongo image","Allocate more CPU/memory to Docker (or the CI runner) to speed up replica set election","Enable container log following to diagnose why the master never becomes primary"],"exampleFix":"// before\nnew MongoDBContainer(\"mongo:3.6\") // old image, flaky election on busy CI\n// after\nnew MongoDBContainer(DockerImageName.parse(\"mongo:6.0\"));\n// and ensure the runner has enough CPU; rerun if transient","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    mongo.start();\n} catch (ReplicaSetInitializationException e) {\n    if (e.getMessage().contains(\"was not initialized\")) {\n        mongo.stop();\n        mongo.start(); // retry once; election delays are often transient\n    } else {\n        throw e;\n    }\n}","preventionTips":["Provision enough CPU/memory for Docker on CI runners","Use a recent, supported mongo image version","Rerun flaky init failures before treating them as code bugs"],"tags":["mongodb","testcontainers","replica-set","timeout"],"backgroundTag":"request-timeout","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}