{"record":{"id":"f2293f213f47f0dd","repo":"LMAX-Exchange/disruptor","slug":"n-must-be-0","errorCode":null,"errorMessage":"n must be > 0","messagePattern":"n must be > 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/lmax/disruptor/MultiProducerSequencer.java","lineNumber":156,"sourceCode":"\n    /**\n     * @see Sequencer#tryNext()\n     */\n    @Override\n    public long tryNext() throws InsufficientCapacityException\n    {\n        return tryNext(1);\n    }\n\n    /**\n     * @see Sequencer#tryNext(int)\n     */\n    @Override\n    public long tryNext(final int n) throws InsufficientCapacityException\n    {\n        if (n < 1)\n        {\n            throw new IllegalArgumentException(\"n must be > 0\");\n        }\n\n        long current;\n        long next;\n\n        do\n        {\n            current = cursor.get();\n            next = current + n;\n\n            if (!hasAvailableCapacity(gatingSequences, n, current))\n            {\n                throw InsufficientCapacityException.INSTANCE;\n            }\n        }\n        while (!cursor.compareAndSet(current, next));\n\n        return next;","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/LMAX-Exchange/disruptor/blob/c871ca49826a6be7ada6957f6fbafcfecf7b1f87/src/main/java/com/lmax/disruptor/MultiProducerSequencer.java#L138-L174","documentation":"Thrown by MultiProducerSequencer.tryNext(int n) when n < 1. Unlike next(n), tryNext only rejects non-positive claims; claiming more than the buffer is fine here because it fails atomically with InsufficientCapacityException instead of blocking. A zero/negative n would corrupt the claim arithmetic, so it is rejected.","triggerScenarios":"Calling ringBuffer.tryNext(0) or tryNext(negativeValue), typically with a dynamically computed batch size such as tryNext(remaining) where remaining hits 0 in a loop.","commonSituations":"A drain loop that computes 'how many to claim' and hits 0 on its last iteration; consumer-side batching math that underflows; reusing a size variable after it has been decremented to 0.","solutions":["Check n >= 1 before calling tryNext; break out of the claiming loop when the computed count reaches 0.","Use the single-slot form ringBuffer.tryNext() (equivalent to tryNext(1)) when claiming one event.","Unit-test the boundary iteration where the remaining count becomes 0."],"exampleFix":"// before\nlong seq = ringBuffer.tryNext(remaining); // remaining == 0 on last loop pass\n\n// after\nif (remaining < 1) break;\nlong seq = ringBuffer.tryNext(remaining);","handlingStrategy":"validation","validationCode":"if (n >= 1) {\n    long seq = ringBuffer.tryNext(n);\n    // publish\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Break claiming loops when the computed count drops below 1.","Prefer the no-arg tryNext() for single events."],"tags":["disruptor","producer","batching","validation","try-next"],"backgroundTag":null,"analyzedSha":"c871ca49826a6be7ada6957f6fbafcfecf7b1f87","analyzedAt":"2026-08-14T14:22:36.358Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}