aeron-io/aeron · error · IllegalArgumentException

invalid block length , remaining space in term is

Error message

invalid block length , remaining space in term is 

What it means

ExclusivePublication's internal checkBlockLength() throws this IllegalArgumentException when a block write (e.g. tryAppendBlock or similar API) requests more bytes than remain in the current term at termOffset. A block must fit entirely within the term buffer, otherwise frames would straddle the term boundary, which the protocol forbids.

Solutions

  1. Cap the block length to the remaining space in the term before appending (query termBufferLength and termOffset).
  2. Retry the append after the term rotates (when termOffset resets).
  3. Increase the channel term-length so the block fits within a single term.

Example fix

// before
publication.tryAppendBlock(buffer, offset, blockLength);
// after
int remaining = publication.termBufferLength() - publication.termOffset();
if (blockLength <= remaining) {
    publication.tryAppendBlock(buffer, offset, blockLength);
}
Defensive patterns

Strategy: validation

Validate before calling

int remaining = publication.termBufferLength() - publication.termOffset();
if (blockLength > remaining) {
    // defer write until after term rotation
}

Try / catch

try {
    publication.tryAppendBlock(buffer, offset, blockLength);
} catch (IllegalArgumentException e) {
    // shrink block or wait for next term
}

Prevention

When it happens

Trigger: Appending a block whose length exceeds termBufferLength - termOffset, i.e. near the end of a term with an oversized block, or passing a length larger than the whole term buffer.

Common situations: Writing large batches/blocks near term rollover; hard-coded block sizes that exceed a configured term length; switching channels to smaller term lengths without resizing block writes.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of aeron-io/aeron@6d60124e15 (2026-09-12). Data as JSON: /api/errors/6e59c733963c7a5d. Report an issue: GitHub.

Appendix: source

Thrown at aeron-client/src/main/java/io/aeron/ExclusivePublication.java:546

            final int tailCounterOffset = TERM_TAIL_COUNTERS_OFFSET + (activePartitionIndex * SIZE_OF_LONG);
            final UnsafeBuffer termBuffer = termBuffers[activePartitionIndex];
            final int result = appendBlock(termBuffer, tailCounterOffset, buffer, offset, length);

            return newPosition(result);
        }
        else
        {
            return backPressureStatus(position, length);
        }
    }

    private void checkBlockLength(final int length)
    {
        final int remaining = termBufferLength - termOffset;
        if (length > remaining)
        {
            throw new IllegalArgumentException(
                "invalid block length " + length + ", remaining space in term is " + remaining);
        }
    }

    private void checkFirstFrame(final MutableDirectBuffer buffer, final int offset)
    {
        final int frameType = HDR_TYPE_DATA;
        final int blockTermOffset = buffer.getInt(offset + TERM_OFFSET_FIELD_OFFSET, LITTLE_ENDIAN);
        final int blockSessionId = buffer.getInt(offset + SESSION_ID_FIELD_OFFSET, LITTLE_ENDIAN);
        final int blockStreamId = buffer.getInt(offset + STREAM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
        final int blockTermId = buffer.getInt(offset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
        final int blockFrameType = buffer.getShort(offset + TYPE_FIELD_OFFSET, LITTLE_ENDIAN) & 0xFFFF;

        if (blockTermOffset != termOffset ||
            blockSessionId != sessionId ||
            blockStreamId != streamId ||
            blockTermId != termId ||
            frameType != blockFrameType)

View on GitHub (pinned to 6d60124e15)