{"record":{"id":"68e080715659adf6","repo":"apache/hadoop","slug":"already-started","errorCode":null,"errorMessage":"Already started","messagePattern":"Already started","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-minikdc/src/main/java/org/apache/hadoop/minikdc/MiniKdc.java","lineNumber":278,"sourceCode":"   * @return the realm of the MiniKdc.\n   */\n  public String getRealm() {\n    return realm;\n  }\n\n  public File getKrb5conf() {\n    krb5conf = new File(System.getProperty(JAVA_SECURITY_KRB5_CONF));\n    return krb5conf;\n  }\n\n  /**\n   * Starts the MiniKdc.\n   *\n   * @throws Exception thrown if the MiniKdc could not be started.\n   */\n  public synchronized void start() throws Exception {\n    if (simpleKdc != null) {\n      throw new RuntimeException(\"Already started\");\n    }\n    simpleKdc = new SimpleKdcServer();\n    prepareKdcServer();\n    simpleKdc.init();\n    resetDefaultRealm();\n    simpleKdc.start();\n    LOG.info(\"MiniKdc started.\");\n  }\n\n  private void resetDefaultRealm() throws IOException {\n    InputStream templateResource = new FileInputStream(\n            getKrb5conf().getAbsolutePath());\n    String content = IOUtil.readInput(templateResource);\n    content = content.replaceAll(\"default_realm = .*\\n\",\n            \"default_realm = \" + getRealm() + \"\\n\");\n    IOUtil.writeFile(content, getKrb5conf());\n  }\n","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-minikdc/src/main/java/org/apache/hadoop/minikdc/MiniKdc.java#L260-L296","documentation":"MiniKdc.start() is not idempotent. It throws RuntimeException(\"Already started\") when the internal simpleKdc field is already set, i.e. start() was called a second time on the same instance without an intervening stop(). The guard exists because the embedded Kerby SimpleKdcServer cannot be initialized twice.","triggerScenarios":"Calling miniKdc.start() twice on the same instance: a @Before setUp() that starts the KDC while a previous test's stop() was skipped or failed, or helper code that tries to 'restart' the KDC by calling start() again.","commonSituations":"JUnit suites reusing a static MiniKdc across test methods; a failing test aborts @After tearDown so the KDC stays running and the next start() throws; harness code assuming start() is idempotent.","solutions":["Create a fresh MiniKdc instance per test: construct and start() in @Before, stop() in @After (wrapped in try/finally)","Ensure the previous instance is stopped before starting again: stop() clears simpleKdc, after which start() works again on a new instance","If reuse is required, track a started flag in your own harness and skip start() when already running"],"exampleFix":"// before\n@Before public void setUp() { kdc.start(); } // second test -> Already started\n\n// after\nprivate MiniKdc kdc;\n@Before public void setUp() throws Exception {\n  kdc = new MiniKdc(MiniKdc.createConf(), workDir); // fresh instance each test\n  kdc.start();\n}\n@After public void tearDown() { if (kdc != null) kdc.stop(); }","handlingStrategy":"validation","validationCode":"private boolean kdcStarted = false;\n\npublic synchronized void ensureStarted(MiniKdc kdc) throws Exception {\n  if (!kdcStarted) {\n    kdc.start();\n    kdcStarted = true;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  kdc.start();\n} catch (RuntimeException e) {\n  if (\"Already started\".equals(e.getMessage())) {\n    return; // idempotent no-op\n  }\n  throw e;\n}","preventionTips":["Construct a fresh MiniKdc per test in @Before and stop() it in @After with try/finally","Never assume start() is idempotent — track lifecycle state in your harness","Make tearDown resilient: even if a test fails, stop() must run so the next test starts clean"],"tags":["kerberos","minikdc","lifecycle","test-infrastructure"],"backgroundTag":"service-already-started","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}