{"record":{"id":"cdfef1008da64868","repo":"aeron-io/aeron","slug":"counter-not-allocated-id","errorCode":null,"errorMessage":"Counter not allocated: id=","messagePattern":"Counter not allocated: id=","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"aeron-client/src/main/java/io/aeron/status/ReadableCounter.java","lineNumber":52,"sourceCode":"    private final UnsafeBuffer valueBuffer;\n    private final long registrationId;\n    private final int counterId;\n    private volatile boolean isClosed = false;\n\n    /**\n     * Construct a view of an existing counter.\n     *\n     * @param countersReader for getting access to the buffers.\n     * @param registrationId assigned by the driver for the counter or {@link Aeron#NULL_VALUE} if not known.\n     * @param counterId      for the counter to be viewed.\n     * @throws IllegalStateException if the id has for the counter has not been allocated.\n     */\n    public ReadableCounter(final CountersReader countersReader, final long registrationId, final int counterId)\n    {\n        final int counterState = countersReader.getCounterState(counterId);\n        if (counterState != CountersReader.RECORD_ALLOCATED)\n        {\n            throw new IllegalStateException(\"Counter not allocated: id=\" + counterId + \" state=\" + counterState);\n        }\n\n        this.countersReader = countersReader;\n        this.counterId = counterId;\n        this.registrationId = registrationId;\n\n        final AtomicBuffer valuesBuffer = countersReader.valuesBuffer();\n        final int counterOffset = CountersReader.counterOffset(counterId);\n        valuesBuffer.boundsCheck(counterOffset, SIZE_OF_LONG);\n\n        valueBuffer = new UnsafeBuffer(valuesBuffer, counterOffset, SIZE_OF_LONG);\n    }\n\n    /**\n     * Construct a view of an existing counter.\n     *\n     * @param countersReader for getting access to the buffers.\n     * @param counterId      for the counter to be viewed.","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-client/src/main/java/io/aeron/status/ReadableCounter.java#L34-L70","documentation":"The ReadableCounter constructor verifies via countersReader.getCounterState(counterId) that the counter is in RECORD_ALLOCATED state and throws IllegalStateException otherwise. Counters in an Aeron CountersManager can be unallocated, reclaimed, or in transition, so reading a counter that no longer (or never did) exist fails fast here.","triggerScenarios":"Constructing ReadableCounter with a counterId that is RECORD_RECLAIMED, RECORD_UNUSED, or otherwise not RECORD_ALLOCATED — typically a stale id from a driver that has since freed the counter.","commonSituations":"Monitoring a driver that restarted and reallocated counters with different ids; holding a ReadableCounter across a media driver shutdown/restart; racing counter allocation vs. reader construction in Aeron-stat-style tooling.","solutions":["Check countersReader.getCounterState(counterId) == CountersReader.RECORD_ALLOCATED before constructing","Catch IllegalStateException and re-resolve the counterId from the counters labels","Refresh the counters snapshot after a driver restart instead of reusing old ids"],"exampleFix":"// before\nReadableCounter counter = new ReadableCounter(countersReader, registrationId, counterId);\n// after\nif (countersReader.getCounterState(counterId) == CountersReader.RECORD_ALLOCATED)\n{\n    ReadableCounter counter = new ReadableCounter(countersReader, registrationId, counterId);\n}","handlingStrategy":"try-catch","validationCode":"if (countersReader.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED) { /* do not construct */ }","typeGuard":"null","tryCatchPattern":"try { ReadableCounter c = new ReadableCounter(countersReader, registrationId, counterId); }\ncatch (IllegalStateException e) { /* re-resolve counter id from labels */ }","preventionTips":["Re-resolve counter ids after driver restarts","Check counter state before constructing readers","Subscribe to counter lifecycle events instead of caching ids"],"tags":["aeron","counters","lifecycle","state"],"backgroundTag":"invalid-state-transition","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"}