Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8196106

Support nested infinite or recursive flat mapped streams


    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:


      JDK-8075939 fixed the case where a flat map operation contains an infinite stream. However, it did not fix the case where there are nested flat map calls that contain infinite streams via recursion or explicitly.

      For example, here is a recursive example from JDK-8189234:

          IntStream.rangeClosed(0, 1).

          static IntStream getChildren() {
              return IntStream.rangeClosed(0, 1).

          static IntStream map(int c) {
              return IntStream.concat(

      which will result in a StackOverlowError.

      Here is another example using infinite streams:

          IntStream.rangeClosed(i - 1, i + 1).
                  flatMap(c -> IntStream.generate(() -> 1).
                          flatMap(x -> IntStream.generate(() -> x))).

      which will result in an OutOfMemoryError.

      To solve these issues will require a mechanism to propagate the enclosing stream's cancellation function (Sink::cancellationRequested) to nested streams. Such as a terminal operation forEachOrderedWithCancel that accepts a Predicate function to query the enclosing cancellation state.

      This will also avoid an issue with the spliterator obtained from a stream (AbstractWrappingSpliterator and sub-classes) which has to buffer elements to avoid reporting more than one element via Spliterator::tryAdvance (independently the implementation can be improved by directly reporting the first element and buffering subsequent elements).


          Issue Links



              • Assignee:
                psandoz Paul Sandoz
                psandoz Paul Sandoz
              • Votes:
                0 Vote for this issue
                1 Start watching this issue


                • Created: