ReactiveX/RxJava · error · IllegalArgumentException

Overflow! start + count is bigger than Long.MAX_VALUE

Error message

Overflow! start + count is bigger than Long.MAX_VALUE

What it means

Thrown by Flowable.intervalRange when start + (count - 1) would overflow Long.MAX_VALUE. The library computes the inclusive end and detects wraparound by checking that a positive start produced a negative end. It fails fast rather than emitting a corrupted (negative) sequence.

Source

Thrown at src/main/java/io/reactivex/rxjava4/core/Flowable.java:2411

     * @throws IllegalArgumentException
     *             if {@code count} is less than zero, or if {@code start} + {@code count} − 1 exceeds
     *             {@link Long#MAX_VALUE}
     */
    @CheckReturnValue
    @NonNull
    @BackpressureSupport(BackpressureKind.ERROR)
    @SchedulerSupport(SchedulerSupport.CUSTOM)
    public static Flowable<Long> intervalRange(long start, long count, long initialDelay, long period, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) {
        if (count < 0L) {
            throw new IllegalArgumentException("count >= 0 required but it was " + count);
        }
        if (count == 0L) {
            return Flowable.<Long>empty().delay(initialDelay, unit, scheduler);
        }

        long end = start + (count - 1);
        if (start > 0 && end < 0) {
            throw new IllegalArgumentException("Overflow! start + count is bigger than Long.MAX_VALUE");
        }
        Objects.requireNonNull(unit, "unit is null");
        Objects.requireNonNull(scheduler, "scheduler is null");

        return RxJavaPlugins.onAssembly(new FlowableIntervalRange(start, end, Math.max(0L, initialDelay), Math.max(0L, period), unit, scheduler));
    }

    /**
     * Returns a {@code Flowable} that signals the given (constant reference) item and then completes.
     * <p>
     * <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/just.v3.png" alt="">
     * <p>
     * Note that the item is taken and re-emitted as is and not computed by any means by {@code just}. Use {@link #fromCallable(Callable)}
     * to generate a single item on demand (when {@link Subscriber}s subscribe to it).
     * <p>
     * See the multi-parameter overloads of {@code just} to emit more than one (constant reference) items one after the other.
     * Use {@link #fromArray(Object...)} to emit an arbitrary number of items that are known upfront.
     * <p>

View on GitHub (pinned to a8ab535614)

Solutions

  1. Cap or reduce start and/or count so the inclusive end stays <= Long.MAX_VALUE.
  2. Validate the range up front: boolean overflow = start > 0L && Long.MAX_VALUE - (count - 1) < start.
  3. Use a smaller count, or page the emission in chunks.

Example fix

// before
long start = Long.MAX_VALUE - 10;
Flowable.intervalRange(start, 100L, 0, 1, TimeUnit.SECONDS, scheduler);

// after
long start = Long.MAX_VALUE - 10;
long count = Math.min(100L, Long.MAX_VALUE - start + 1);
Flowable.intervalRange(start, count, 0, 1, TimeUnit.SECONDS, scheduler);
Defensive patterns

Strategy: validation

Validate before calling

long end = start + (count - 1);
boolean overflow = start > 0L && end < 0L;
if (overflow) {
    // clamp count so the inclusive end stays within Long.MAX_VALUE
    count = Long.MAX_VALUE - start + 1;
}

Try / catch

try {
    return Flowable.intervalRange(start, count, initialDelay, period, unit, scheduler);
} catch (IllegalArgumentException e) {
    // reduce the range and retry, or signal empty
    return Flowable.empty();
}

Prevention

When it happens

Trigger: Calling Flowable.intervalRange(start, count, ...) with start > 0L and start + (count - 1) exceeding Long.MAX_VALUE (end wraps to negative). Happens with very large start values combined with a large count.

Common situations: Paging over ID ranges near Long.MAX_VALUE; monotonic counters seeded near the top of the long range; computed ranges whose magnitude was never bounded.

Related errors


AI-assisted analysis of ReactiveX/RxJava@a8ab535614 (2026-08-13). Data as JSON: /api/errors/11c0e99e2334ddbd. Report an issue: GitHub.