{"record":{"id":"0a780c4824a0cfeb","repo":"apache/hadoop","slug":"service-is-in-wrong-state","errorCode":null,"errorMessage":"Service {} is in wrong state: {}","messagePattern":"Service (.+?) is in wrong state: (.+?)","errorType":"exception","errorClass":"ServiceStateException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-registry/src/main/java/org/apache/hadoop/registry/client/impl/zk/CuratorService.java","lineNumber":206,"sourceCode":"   */\n  @Override\n  protected void serviceStop() throws Exception {\n    IOUtils.closeStream(curator);\n\n    if (curatorCacheBridge != null) {\n      curatorCacheBridge.close();\n    }\n    super.serviceStop();\n  }\n\n  /**\n   * Internal check that a service is in the live state.\n   *\n   * @throws ServiceStateException if not\n   */\n  private void checkServiceLive() throws ServiceStateException {\n    if (!isInState(STATE.STARTED)) {\n      throw new ServiceStateException(\n          \"Service \" + getName() + \" is in wrong state: \"\n              + getServiceState());\n    }\n  }\n\n  /**\n   * Flag to indicate whether or not the registry is secure.\n   * Valid once the service is inited.\n   *\n   * @return service security policy\n   */\n  public boolean isSecure() {\n    return registrySecurity.isSecureRegistry();\n  }\n\n  /**\n   * Get the registry security helper.\n   *","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-registry/src/main/java/org/apache/hadoop/registry/client/impl/zk/CuratorService.java#L188-L224","documentation":"Every zk*() operation on CuratorService begins with checkServiceLive(), which requires the Hadoop CompositeService to be in STATE.STARTED; any other state (NOTINITED, INITED, STOPPED) throws ServiceStateException('Service <name> is in wrong state: <state>'). It is a lifecycle precondition failure: the Curator/ZooKeeper client does not exist yet, or has been torn down, so the operation is refused before any ZK call is made.","triggerScenarios":"Instantiating CuratorService/RegistryOperationsService with new, calling init(conf), then issuing zkStat/zkCreate/etc. without start(); invoking operations after serviceStop(); issuing calls from another thread while startup is still in progress; calling registry methods from your own service's init phase.","commonSituations":"Not using RegistryOperationsFactory (whose create* methods create, init and start the instance); ordering bugs where a dependent service uses the registry before it starts; reusing a stopped client after shutdown.","solutions":["Create clients via RegistryOperationsFactory.createAnonymousInstance/createKerberosInstance/createAuthenticatedInstance, which start the service before returning it.","If constructing manually, call init(conf) then start() and wait for completion before issuing any operation.","Never reuse an instance after stop(); build and start a fresh one."],"exampleFix":"// before\nCuratorService curator = new CuratorService(\"registry\");\ncurator.init(conf);\ncurator.zStat(\"/services/api\"); // ServiceStateException: wrong state INITED\n\n// after: init + start, or simply\nRegistryOperations ops = RegistryOperationsFactory.createAnonymousInstance(conf);","handlingStrategy":"validation","validationCode":"if (!registryOperations.isInState(STATE.STARTED)) {\n  throw new IllegalStateException(\"Registry client not started; call init(conf) + start() first\");\n}\n// then issue operations","typeGuard":"// narrow to a started service before issuing zk operations\nprivate static boolean isLive(Service service) {\n  return service.isInState(STATE.STARTED);\n}","tryCatchPattern":"try {\n  curatorService.zStat(path);\n} catch (ServiceStateException e) {\n  // lifecycle bug: start the service (or rebuild via RegistryOperationsFactory) before retrying\n}","preventionTips":["Always build registry clients with RegistryOperationsFactory — it creates, inits and starts in one call.","Do not issue registry operations from your own serviceInit(); wait until serviceStart has completed.","Never reuse a stopped client; construct and start a new instance instead."],"tags":["registry","zookeeper","service-lifecycle","state-machine","hadoop-registry"],"backgroundTag":"service-not-started","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}