Search code examples
javarx-javarx-java2

SwitchIfEmpty Chain results in StackOverflowError


The following code results in a StackOverflowError. Is this supposed to happen when writing code like this or is it something that needs to be addressed in the framework?

The code has been tested with rxjava-2.2.19. Sources can be found here.

Sample

The following code is based on code we use to accomplish some sort of looping with a break condition (see flowable = flowable.switchIfEmpty(third(s))). Apparently this approach is not working quite well.

public class SwitchIfEmptyTest {

    public static void main(String[] args) {
        SwitchIfEmptyDemo demo = new SwitchIfEmptyDemo();
        demo.one("foo")
                .blockingForEach(s -> System.out.println(s));
    }

    static class SwitchIfEmptyDemo {

        private SomeSource source = new SomeSource();

        public Flowable<String> one(String input) {
            return Flowable.<String>empty()
                    .switchIfEmpty(two(input));
        }

        public Flowable<String> two(String input) {
            return Flowable.<String>create(emitter -> {
                emitter.onNext(input);
                emitter.onComplete();
            }, BackpressureStrategy.ERROR)
                    .flatMap(inputFlowable -> {
                        return source.read()
                                .toList()
                                .toFlowable()
                                .flatMap(strings -> {
                                    Flowable<String> flowable = Flowable.empty();
                                    for (String s : strings) {
                                        flowable = flowable.switchIfEmpty(third(s));
                                    }
                                    return flowable;
                                });
                    });
        }

        public Flowable<String> third(String input) {
            //System.out.println("Value " + input);
            return Flowable.empty();
        }
    }

    static class SomeSource {

        public Flowable<String> read() {
            return Flowable.create(emitter -> {
                for (int i = 0; i < 1_000_000; i++) {
                    emitter.onNext("Some values " + i);
                }
                emitter.onComplete();
            }, BackpressureStrategy.ERROR);
        }
    }
}

Stacktrace

Exception in thread "main" java.lang.StackOverflowError
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at io.reactivex.Flowable.subscribe(Flowable.java:14939)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
...

Solution

  • In order to get rid of the StackOverflowError, one possibility is to use the takeWhile operator (see full diff).

    public Flowable<String> two(String input) {
        return Flowable.<String>create(emitter -> {
            emitter.onNext(input);
            emitter.onComplete();
        }, BackpressureStrategy.ERROR)
                .flatMap(inputFlowable -> {
                    return source.read()
                            .flatMap(this::third)
                            .takeWhile(s -> s.equals("false"));
                });
    }