{"record":{"id":"ef49850bccd1f156","repo":"apache/druid","slug":"i-m-being-asked-to-become-leader-but-the-latch-is","errorCode":null,"errorMessage":"I'm being asked to become leader, but the latch is CLOSED. Ignored event.","messagePattern":"I'm being asked to become leader, but the latch is CLOSED\\. Ignored event\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/src/main/java/org/apache/druid/curator/discovery/CuratorDruidLeaderSelector.java","lineNumber":93,"sourceCode":"\n  private LeaderLatch createNewLeaderLatch()\n  {\n    return new LeaderLatch(curator, latchPath, self.getServiceScheme() + \"://\" + self.getHostAndPortToUse());\n  }\n\n  private LeaderLatch createNewLeaderLatchWithListener()\n  {\n    final LeaderLatch newLeaderLatch = createNewLeaderLatch();\n\n    newLeaderLatch.addListener(\n        new LeaderLatchListener()\n        {\n          @Override\n          public void isLeader()\n          {\n            try {\n              if (newLeaderLatch.getState().equals(LeaderLatch.State.CLOSED)) {\n                log.warn(\"I'm being asked to become leader, but the latch is CLOSED. Ignored event.\");\n                return;\n              }\n\n              if (leader) {\n                log.warn(\"I'm being asked to become leader. But I am already the leader. Ignored event.\");\n                return;\n              }\n\n              leader = true;\n              term.incrementAndGet();\n              listener.becomeLeader();\n            }\n            catch (Exception ex) {\n              log.makeAlert(ex, \"listener becomeLeader() failed. Unable to become leader\").emit();\n              notLeader();\n            }\n          }\n","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/server/src/main/java/org/apache/druid/curator/discovery/CuratorDruidLeaderSelector.java#L75-L111","documentation":"CuratorDruidLeaderSelector's internal LeaderLatchListener.isLeader() was invoked by Curator while the LeaderLatch had already been closed (State.CLOSED) during a latch swap or shutdown. The callback is ignored and a warning logged, because leadership cannot be accepted on a closed latch.","triggerScenarios":"A leadership-granted event delivered concurrently with stopAndCreateNewLeaderLatch() (triggered by a prior notLeader event) or with unregisterListener()/service shutdown — Curator's listener fires after the old latch was closed.","commonSituations":"Rapid leader flapping (frequent leadership loss/regain), ZK session instability causing latch recreation, or server shutdown while a leadership grant is still in flight.","solutions":["No action needed if it appears during shutdown or latch recreation — the event is intentionally ignored.","If it recurs during normal operation, investigate ZK session churn (session timeouts, connectivity) that forces latch recreation.","Ensure unregisterListener()/lifecycle stop is not called before the service finishes its leader work.","Upgrade Curator/Druid if the latch state race causes missed leadership (stale latch never replaced)."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// events on a closed latch are intentionally dropped; guard your leader-side effects\n@Override\npublic void becomeLeader() {\n  if (!started) { return; } // ignore callbacks after service stop\n  doLeaderWork();\n}","preventionTips":["Keep ZK sessions stable; tune session timeout for your network.","Avoid stopping the leader selector while leadership grants are in flight.","Log latch lifecycle events to correlate this warning with latch swaps."],"tags":["zookeeper","curator","leader-election","race-condition"],"backgroundTag":"invalid-state-transition","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}