RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

1.x Single#flatMapCompletable calls hooks onError when mapping to an error completeable

Open DeepSpawn opened this issue 9 years ago • 2 comments

If you flatmap a Single into a Completeable, and the Completeable is an error, you will have the exception passed into the RxJavaHooks.onError global catch function as well as ending up with the Completeable Error you expect to see.

I have managed to get is down to the follow test case highlighting the problem,

@Test
    public void flatMapCompletableErrorCallsHooksErrorHandler() {
        AtomicBoolean wasHooksHandlerCalled = new AtomicBoolean();
        RxJavaHooks.setOnError(t -> wasHooksHandlerCalled.set(true));

        Completable c3  = Single.just(1)
                .flatMapCompletable(i -> Completable.error(new RuntimeException("some exception")));

        c3.get();

        assertThat(wasHooksHandlerCalled.get(), is(false)); //fails
    }

I did some digging and I think this is happening due to a bug in SafeCompletableSubscriber, where it calls RxJavaHooks.onError(e) before it attempts to pass the error in to the wrapped subscribers onError method. ie

public void onError(Throwable e) {
        RxJavaHooks.onError(e);
        if (done) {
            return;
        }
        done = true;
        try {
            actual.onError(e);
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);

            throw new OnErrorFailedException(new CompositeException(e, ex));
        }
    }

Looking at SafeSubscriber, I think the correct behaviour is to only have the call to RxJavaHooks.onError(e); happen inside the catch block, as no exception has leaked at the point where it is currently being called.

DeepSpawn avatar Jul 06 '17 07:07 DeepSpawn

Actually, it is supposed to be inside the if (done) { } block. Would you like to submit a PR?

akarnokd avatar Jul 06 '17 07:07 akarnokd

Closing via #5470.

akarnokd avatar Jul 19 '17 09:07 akarnokd