Skip to content

FAQ: UndeliverableException

Dariusz Seweryn edited this page May 28, 2019 · 4 revisions

After the migration RxJava1 => RxJava2 there were reports of app crashes due to an UndeliverableException.

The root cause of the problem is the threading of Android OS — callbacks may be called on arbitrary threads. The library does not know how it will be used and thus it is routing errors to all listeners. Because the errors sometimes may be routed through many merged observables they may appear more than once in an observable chain...

One of the differences between RxJava 1 and RxJava 2 is that the former did swallow exceptions that happened after the chain was unsubscribed whereas the latter is throwing an UndeliverableException in this situation. With the current library architecture it is impossible to mitigate this behaviour and the suggested workaround is to add a custom RxJava error handler:

Kotlin

RxJavaPlugins.setErrorHandler { throwable ->
    if (throwable is UndeliverableException && throwable.cause is BleException) {
        return@setErrorHandler // ignore BleExceptions as they were surely delivered at least once
    }
    // add other custom handlers if needed
    throw RuntimeException("Unexpected Throwable in RxJavaPlugins error handler", throwable)
}

Java

RxJavaPlugins.setErrorHandler(throwable -> {
    if (throwable instanceof UndeliverableException && throwable.getCause() instanceof BleException) {
        return; // ignore BleExceptions as they were surely delivered at least once
    }
    // add other custom handlers if needed
    throw new RuntimeException("Unexpected Throwable in RxJavaPlugins error handler", throwable);
});