{"record":{"id":"cd32316dc4547df9","repo":"aeron-io/aeron","slug":"invalid-election-state-counter-code-code","errorCode":null,"errorMessage":"invalid election state counter code: ${code}","messagePattern":"invalid election state counter code: (.+?)","errorType":"exception","errorClass":"ClusterException","httpStatus":null,"severity":"error","filePath":"aeron-cluster/src/main/java/io/aeron/cluster/ElectionState.java","lineNumber":151,"sourceCode":"     *\n     * @return code stored in a {@link io.aeron.Counter} to represent the election state.\n     */\n    public int code()\n    {\n        return code;\n    }\n\n    /**\n     * Get the enum value for a given code stored in a counter.\n     *\n     * @param code representing election state.\n     * @return the enum value for a given code stored in a counter.\n     */\n    public static ElectionState get(final long code)\n    {\n        if (code < 0 || code > (STATES.length - 1))\n        {\n            throw new ClusterException(\"invalid election state counter code: \" + code);\n        }\n\n        return STATES[(int)code];\n    }\n\n    /**\n     * Get the {@link ElectionState} value based on the value stored in an {@link AtomicCounter}.\n     *\n     * @param counter to read the value for matching against {@link #code()}.\n     * @return the {@link ElectionState} value based on the value stored in an {@link AtomicCounter}.\n     */\n    public static ElectionState get(final AtomicCounter counter)\n    {\n        if (counter.isClosed())\n        {\n            return CLOSED;\n        }\n","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-cluster/src/main/java/io/aeron/cluster/ElectionState.java#L133-L169","documentation":"ElectionState.get(long) maps the integer value stored in the election state counter back to an ElectionState enum. The counter must hold a code between 0 and STATES.length-1; any other value cannot correspond to a valid election state, so a ClusterException is thrown.","triggerScenarios":"Calling ElectionState.get with a raw counter value outside the valid range — typically when reading the election state counter after corruption, or passing a sentinel/negative value (e.g. NULL_VALUE) read from an uninitialized or reset counter.","commonSituations":"Reading the election counter before the election is initialized, code that stores a wrong value into the counter, or tools/dashboards reading the counter concurrently with it being closed and returning -1.","solutions":["Validate the counter value is 0..ElectionState.STATES.length-1 before calling get","Check that the election state counter was properly initialized/allocated before being read","Guard against reading a closed counter (counter.isClosed()) and treat that as 'no election running'","If you wrote the value yourself, fix the constant used to set the counter"],"exampleFix":"// before\nElectionState state = ElectionState.get(counter.get());\n// after\nlong code = counter.isClosed() ? -1 : counter.get();\nElectionState state = (code >= 0 && code < ElectionState.values().length)\n    ? ElectionState.get(code)\n    : ElectionState.INIT;","handlingStrategy":"validation","validationCode":"long code = counter.isClosed() ? -1 : counter.get(); if (code < 0 || code >= ElectionState.values().length) return null;","typeGuard":"boolean isValidElectionCode(long c) { return c >= 0 && c < ElectionState.values().length; }","tryCatchPattern":"try { state = ElectionState.get(code); } catch (ClusterException e) { state = ElectionState.INIT; }","preventionTips":["Only set the counter with ElectionState.code() values","Check counter.isClosed() before reads","Initialize the counter before any readers start"],"tags":["aeron-cluster","election","enum","counter"],"backgroundTag":"invalid-enum-value","analyzedSha":"6d60124e15e35c11b49ba2e3c2c2858a09a18803","analyzedAt":"2026-09-12T11:17:07.683Z","contentChangedAt":"2026-09-12T11:17:07.683Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}