Skip to content

Multiple errors in zip'ed observables with flatMap #6487

Closed
@bitvale

Description

@bitvale

While reading Error handling on wiki I realized that in a zip operator with a multiple network calls, it's possible to receive a crash with an UndeliverableException. I tried to reproduce this behavior:

RxJava version: 2.2.0

Reproduce code:

    @Test
    public void testUncaughtException() throws InterruptedException {
        Observable first = Observable.create(e -> {
            System.out.println("first");
            throw new HelperException("first exception");
        });

        Observable second = Observable.create(e -> {
            System.out.println("second");
            throw new HelperException("second exception");
        });

        List<Observable<?>> observableList = new ArrayList<>();
        observableList.add(first);
        observableList.add(second);

        Observable.zip(observableList, objects -> "result")
                .subscribeOn(Schedulers.io())
                .subscribe(
                        System.out::println,
                        t -> {
                            System.out.println("exception caught!");
                        }
                );
        Thread.sleep(2000);
    }

The output as expected:

first
exception caught!
second
io.reactivex.exceptions.UndeliverableException: ... HelperException: second exception ...

And the second test:

    @Test
    public void testUncaughtExceptionWithFlatMap() throws InterruptedException {
        Observable testObservable = Observable.create(e -> e.onNext(""))
                .flatMap((Function<Object, ObservableSource<?>>) o -> {

                    Observable first = Observable.create(e -> {
                        System.out.println("first");
                        throw new HelperException("first exception");
                    });

                    Observable second = Observable.create(e -> {
                        System.out.println("second");
                        throw new HelperException("second exception");
                    });

                    List<Observable<?>> observableList = new ArrayList<>();
                    observableList.add(first);
                    observableList.add(second);

                    return Observable.zip(observableList, objects -> "result");
                });

        testObservable
                .subscribeOn(Schedulers.io())
                .subscribe(
                        System.out::println,
                        t -> System.out.println("exception caught!")
                );

        Thread.sleep(2000);
    }

And I expected an UndeliverableException too, but the output is:

first
exception caught!

Is this behavior correct? Why there is no UndeliverableException in the second test?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions