rayon-rs/rayon · error

overflow

Error message

overflow

What it means

`Chain`'s `ExactSizeIterator::len()` adds the lengths of both inner iterators with `checked_add` and panics on usize overflow. The combined parallel iterator claims to be longer than `usize::MAX`, which cannot be represented.

Solutions

  1. Avoid chaining iterators whose combined length approaches usize::MAX
  2. Reduce range sizes or step strides before chaining
  3. Use `chain` on iterators with exact but small lengths, or unindexed sources
  4. Check lengths first: if `a.len() > usize::MAX - b.len()`, restructure

Example fix

// before
let it = (0..usize::MAX).step_by(2).chain((0..usize::MAX).step_by(2));
// after
let it = (0..usize::MAX / 4).step_by(2).chain((0..usize::MAX / 4).step_by(2));
Defensive patterns

Strategy: validation

Validate before calling

fn chain_fits(a: usize, b: usize) -> bool { a.checked_add(b).is_some() }

Prevention

When it happens

Trigger: `chain(a, b).len()` (or any indexed parallel op on it) where `a.len() + b.len()` overflows usize — only feasible with extreme exact-size iterators (e.g. huge ranges with large item strides like `(0..usize::MAX).step_by(k)`).

Common situations: Constructing iterators over near-usize::MAX elements and chaining them; synthetic/benchmark iterators with huge lengths.

Related errors


AI-assisted analysis of rayon-rs/rayon@ee0a00bdb1 (2026-09-07). Data as JSON: /api/errors/920c338c15ab72d4. Report an issue: GitHub.

Appendix: source

Thrown at src/iter/chain.rs:72

}

impl<A, B> IndexedParallelIterator for Chain<A, B>
where
    A: IndexedParallelIterator,
    B: IndexedParallelIterator<Item = A::Item>,
{
    fn drive<C>(self, consumer: C) -> C::Result
    where
        C: Consumer<Self::Item>,
    {
        let Chain { a, b } = self;
        let (left, right, reducer) = consumer.split_at(a.len());
        let (a, b) = join(|| a.drive(left), || b.drive(right));
        reducer.reduce(a, b)
    }

    fn len(&self) -> usize {
        self.a.len().checked_add(self.b.len()).expect("overflow")
    }

    fn with_producer<CB>(self, callback: CB) -> CB::Output
    where
        CB: ProducerCallback<Self::Item>,
    {
        let a_len = self.a.len();
        return self.a.with_producer(CallbackA {
            callback,
            a_len,
            b: self.b,
        });

        struct CallbackA<CB, B> {
            callback: CB,
            a_len: usize,
            b: B,
        }

View on GitHub (pinned to ee0a00bdb1)