SwitchIfEmpty链导致StackOverflowError

问题描述

以下代码导致StackOverflowError。编写这样的代码时应该发生这种情况吗?还是在框架中需要解决

代码已经过rxjava-2.2.19的测试。来源here

样本

下面的代码基于我们用来完成某种带有中断条件的循环的代码(请参见flowable = flowable.switchIfEmpty(third(s)))。显然,这种方法效果不佳。

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)
...

解决方法

为了摆脱StackOverflowError,一种可能性是使用takeWhile运算符(请参阅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"));
            });
}