{"record":{"id":"667457584b31c0ac","repo":"aeron-io/aeron","slug":"counter-id-is-not-allocated","errorCode":null,"errorMessage":"Counter id is not allocated: ","messagePattern":"Counter id is not allocated: ","errorType":"exception","errorClass":"AeronException","httpStatus":null,"severity":"error","filePath":"aeron-client/src/main/java/io/aeron/Counter.java","lineNumber":80,"sourceCode":"        this.registrationId = registrationId;\n        this.clientConductor = clientConductor;\n        this.clientOwned = clientOwned;\n    }\n\n    /**\n     * Construct a read-write view of an existing counter.\n     *\n     * @param countersReader for getting access to the buffers.\n     * @param counterId      for the counter to be viewed.\n     * @throws AeronException if the id has for the counter has not been allocated.\n     */\n    public Counter(final CountersReader countersReader, final int counterId)\n    {\n        super(countersReader.valuesBuffer(), counterId);\n\n        if (countersReader.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED)\n        {\n            throw new AeronException(\"Counter id is not allocated: \" + counterId);\n        }\n\n        correlationId = Aeron.NULL_VALUE;\n        registrationId = countersReader.getCounterRegistrationId(counterId);\n        clientConductor = null;\n        clientOwned = true;\n    }\n\n    /**\n     * Return the correlation id of the counter creation command sent to the media driver.\n     *\n     * @return the correlation id of the command or {@link Aeron#NULL_VALUE} if unknown.\n     */\n    public long correlationId()\n    {\n        return correlationId;\n    }\n","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-client/src/main/java/io/aeron/Counter.java#L62-L98","documentation":"This Counter constructor wraps an existing counter by id and requires it to be in RECORD_ALLOCATED state in the CountersReader. If the id is unallocated or freed, the constructor throws immediately because a Counter cannot safely reference unallocated storage.","triggerScenarios":"Constructing new Counter(countersReader, id) with an id that was never allocated, was already freed, or whose record state is RECLAIMED/unused; using a stale counter id captured before the counter was released.","commonSituations":"Reusing a counter id stored from a previous session; racing with another component that freed the counter; hard-coded or off-by-one counter ids; attaching to a driver whose counters were reset.","solutions":["Verify the counter id with countersReader.getCounterState(id) == RECORD_ALLOCATED before constructing","Obtain the id from the allocation call's return value (Counter allocated by CountersManager) rather than guessing","Re-read the counter id from the current counters snapshot if the client reconnected","Check that the counter wasn't freed (e.g. via counter.close() or driver reclaim) before use"],"exampleFix":"// before\nCounter counter = new Counter(countersReader, savedId);\n\n// after\nif (countersReader.getCounterState(savedId) == CountersReader.RECORD_ALLOCATED)\n{\n    Counter counter = new Counter(countersReader, savedId);\n}\nelse\n{\n    // re-allocate or look up the current id\n}","handlingStrategy":"validation","validationCode":"public static Counter safeCounter(CountersReader reader, int id) {\n    if (reader.getCounterState(id) != CountersReader.RECORD_ALLOCATED) {\n        throw new IllegalArgumentException(\"counter id \" + id + \" is not allocated\");\n    }\n    return new Counter(reader, id);\n}","typeGuard":"boolean isAllocated(CountersReader reader, int id) { return reader.getCounterState(id) == CountersReader.RECORD_ALLOCATED; }","tryCatchPattern":"try { Counter c = new Counter(countersReader, id); } catch (AeronException e) { if (e.getMessage().startsWith(\"Counter id is not allocated\")) { id = relookupOrAllocateCounter(); } else { throw e; } }","preventionTips":["Always source counter ids from the allocation API, never hard-code them","Check getCounterState(id) == RECORD_ALLOCATED before wrapping an id","Refresh stored ids after client reconnects or driver restarts","Track counter lifecycles to avoid using ids after free"],"tags":["aeron","counter","invalid-argument","state"],"backgroundTag":"resource-not-found","analyzedSha":"6d60124e15e35c11b49ba2e3c2c2858a09a18803","analyzedAt":"2026-09-12T11:17:07.683Z","contentChangedAt":"2026-09-12T11:17:07.683Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}