RxAndroid icon indicating copy to clipboard operation
RxAndroid copied to clipboard

CompositeException Behavior

Open benjchristensen opened this issue 12 years ago • 21 comments

There are possibly improvements that need to be made specific to Android related to CompositeException.

History of the discussion and work on this can be found at https://github.com/ReactiveX/RxJava/issues/1405

benjchristensen avatar Oct 07 '14 22:10 benjchristensen

I've moved to io.reactivex:rxandroid:0.23.0 and still see this issue. @mttkay have you found something?

tomrozb avatar Nov 21 '14 15:11 tomrozb

No. I was able to reproduce it and got stuck by the time Android was starting to call into native code.

Any suggestions for how to even debug this welcome! On Nov 21, 2014 4:12 PM, "Tomasz Rozbicki" [email protected] wrote:

I've moved to io.reactivex:rxandroid:0.23.0 and still see this issue. @mttkay https://github.com/mttkay have you found something?

— Reply to this email directly or view it on GitHub https://github.com/ReactiveX/RxAndroid/issues/21#issuecomment-63983021.

mttkay avatar Nov 21 '14 15:11 mttkay

Here's what I've got so far. Sometimes I can find in the crash reports stack trace like below:

16:13:01.654                         E  Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
16:13:01.654                         E      at android.util.Log.getStackTraceString(Log.java:410)
16:13:01.654                         E      at android.util.Slog.e(Slog.java:151)
16:13:01.654                         E      at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:85)
16:13:01.654                         E      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
16:13:01.654                         E      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
16:13:01.654                         E      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:56)
16:13:01.654                         E      ... 9 more
16:13:01.654                         E  Caused by: java.net.ConnectException: failed to connect to /192.168.2.10 (port 8080) after 20000ms: isConnected failed: ECONNREFUSED (Connection refuse
                                        d)
16:13:01.654                         E      at libcore.io.IoBridge.isConnected(IoBridge.java:223)
16:13:01.654                         E      at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
16:13:01.654                         E      at libcore.io.IoBridge.connect(IoBridge.java:112)
...

The most interesting part is: android.util.Log.getStackTraceString(Log.java:410) Looks like this class has been modified by manufacturer, the original one in API21 has only 225 lines, but in fact we don't know if the method implementation is different or not.

Now a simple test, put this code in the onError callback (You can use whatever logging utility you want). The seconds method is from Guava.

UnknownHostException hostException = new UnknownHostException("test");
Timber.e("Log", Log.getStackTraceString(hostException));
Timber.e("Guava", Throwables.getStackTraceAsString(hostException));

And here's the output. Nothing is printed from android.util.Log.getStackTraceString method. I have only Samsung devices, could somebody test this on Nexus with KitKat or at least confirm this on another Samsung device? I've tested it on S4, S5 and Note 3 all with 4.4.2.

16:43:51.151              SomeClass  E  Log                                                                                                                                                    
16:43:51.181                         E  Guava java.net.UnknownHostException: test                                                                                                              
                                            ...                                                               
16:43:51.181                         E      at rx.Observable$33.onError(Observable.java:7307)                                                                                                  
16:43:51.181                         E      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:127)                                                                                   
16:43:51.181                         E      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:96)                                                                                     
16:43:51.181                         E      at rx.internal.operators.NotificationLite.accept(NotificationLite.java:147)                                                                        
16:43:51.181                         E      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:177)                                               
16:43:51.181                         E      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.access$000(OperatorObserveOn.java:65)                                               
16:43:51.181                         E      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:153)                                                  
16:43:51.181                         E      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:45)                                                                             
16:43:51.181                         E      at android.os.Handler.handleCallback(Handler.java:733)                                                                                             
16:43:51.181                         E      at android.os.Handler.dispatchMessage(Handler.java:95)                                                                                             
16:43:51.181                         E      at android.os.Looper.loop(Looper.java:136)                                                                                                         
16:43:51.181                         E      at android.app.ActivityThread.main(ActivityThread.java:5586)                                                                                       
16:43:51.181                         E      at java.lang.reflect.Method.invokeNative(Native Method)                                                                                            
16:43:51.181                         E      at java.lang.reflect.Method.invoke(Method.java:515)                                                                                                
16:43:51.181                         E      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)                                                                
16:43:51.181                         E      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)                                                                                   
16:43:51.181                         E      at dalvik.system.NativeStart.main(Native Method)  

tomrozb avatar Nov 21 '14 15:11 tomrozb

The reason of the exception (in my case) is this line in SafeSubscriber.java line 174 throw new OnErrorFailedException("Error occurred when trying to propagate error to Observer.onError", new CompositeException(Arrays.asList(e, e2))); It throws exception because e2 is null. In my case, the Throwable I pass to onError() method is android.os.NetworkOnMainThreadException

I think a buffer solution would be checking if e2 is null.

I'm using rxandroid 0.23 and Android 5.0.1 x86 emulator. Hope this can help.

gderaco avatar Jan 01 '15 01:01 gderaco

UnknownHostException's trace is never logged, FYI, as you can see in the source of Log.java. Is there still an issue beyond that?

JakeWharton avatar Jul 01 '15 01:07 JakeWharton

I was getting a similar error whenever I used RetrofitError.getBody inside RxJava Subscriber.onError. Turns out it was being caused by the getBody call itself throwing an exception. Since initCause, setStackTrace, readObject, and addSuppressed all internally throw exceptions - maybe it's related.

malachid avatar Aug 12 '15 16:08 malachid

I think this issue is mentioned here http://stackoverflow.com/questions/24860942/observer-onerror-firing-off-inconsistently.

farmazon3000 avatar Sep 22 '15 10:09 farmazon3000

@farmazon3000 you're right. I'm not sure it still happens in the latest version of Rx, can somebody confirm?

tomrozb avatar Sep 23 '15 08:09 tomrozb

I'm using

compile 'io.reactivex:rxjava:1.0.12'
compile 'io.reactivex:rxandroid:0.24.0'

and it does happen.

farmazon3000 avatar Sep 23 '15 09:09 farmazon3000

Those aren't the latest versions of RxJava and RxAndroid. The latest are:

compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'

dlew avatar Sep 23 '15 11:09 dlew

I am on the latest version

  E/AndroidRuntime: java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
  E/AndroidRuntime:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
  E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
  E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
  E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
  E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
  E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
  E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
  E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
  E/AndroidRuntime:  Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
  E/AndroidRuntime:     at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:201)
  E/AndroidRuntime:     at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
  E/AndroidRuntime:     at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:137)
  E/AndroidRuntime:     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:208)
  E/AndroidRuntime:     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:170)
  E/AndroidRuntime:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
  E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733) 
  E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
  E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
  E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
  E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
  E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
  E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
  E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
  E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
  E/AndroidRuntime:  Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 
  E/AndroidRuntime:     at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:201) 
  E/AndroidRuntime:     at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111) 
  E/AndroidRuntime:     at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:137) 
  E/AndroidRuntime:     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:208) 
  E/AndroidRuntime:     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:170) 
  E/AndroidRuntime:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
  E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733) 
  E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
  E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
  E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
  E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
  E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
  E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
  E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
  E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
  E/AndroidRuntime:  Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
  E/AndroidRuntime:     at crittercism.android.bk.a(Unknown Source)
  E/AndroidRuntime:     at crittercism.android.bk.<init>(Unknown Source)
  E/AndroidRuntime:     at crittercism.android.az.a(Unknown Source)
  E/AndroidRuntime:     at crittercism.android.ay.uncaughtException(Unknown Source)```

lunf avatar Sep 30 '15 07:09 lunf

Looks like it has nothing to do with RxAndroid. Your app's code threw an exception (most likely in your onError handler):

E/AndroidRuntime:     at crittercism.android.bk.a(Unknown Source)
E/AndroidRuntime:     at crittercism.android.bk.<init>(Unknown Source)
E/AndroidRuntime:     at crittercism.android.az.a(Unknown Source)
E/AndroidRuntime:     at crittercism.android.ay.uncaughtException(Unknown Source)

dimsuz avatar Sep 30 '15 08:09 dimsuz

I just got it again (this time because I had a TextInputLayout without a child EditText) - but at least I knew what to look for. It would be nice if my own code showed up in the stack trace somewhere. Without that it turns out to be much harder to track down.

Key though - it's always because your onError is calling something that is throwing an error of some kind (may be runtime, error, etc - not necessarily a compile time exception).

malachid avatar Sep 30 '15 14:09 malachid

This issue is old, and has no clear action to take. The most recent comments are normal errors being incorrectly attributed to this issue. As far as I can tell CE is now behaving in an acceptable manner and even if it wasn't this isn't the project which could change it.

JakeWharton avatar May 01 '16 07:05 JakeWharton

We're experiencing this issue also in our code. Clearly, its an application issue where somewhere the onError implementation throws. However, RxJava makes it hard to point out where exactly this happens, because CompositeException swallows onErrors exception iff it shares a cause with the original throwable. Here is the code:

List<Throwable> listOfCauses = getListOfCauses(e);
// check if any of them have been seen before
for(Throwable child : listOfCauses) {
    if (seenCauses.contains(child)) {
        // already seen this outer Throwable so skip
        e = new RuntimeException("Duplicate found in causal chain so cropping to prevent loop ...");
        continue;
    }
    seenCauses.add(child);
}

So, say the chain throws e1 and your code in onError throws e2 while being "somehow" a good citizen and remembers the original e1 as cause. e2 will then completely get evicted / replaced with a new RuntimeException telling us "Duplicate found in causal chain so cropping to prevent loop ...". If you then work with Crashlytics, you will not see anything beside this very runtime exception (even without the exception message, since this is not part of Crashlytics' output for causes for whatever reason). And you will not see anything on logcat, because the exception chain is already way too long.

realdadfish avatar May 23 '16 17:05 realdadfish

Can we create a very small sample (preferably without using anything Android-specific) that reproduces the problem?

On Mon, May 23, 2016 at 1:23 PM Thomas Keller [email protected] wrote:

We're experiencing this issue also in our code. Clearly, its an application issue where somewhere the onError implementation throws. However, RxJava makes it hard to point out where exactly this happens, because CompositeException swallows onErrors exception iff it shares a cause with the original throwable. Here is the code:

        List<Throwable> listOfCauses = getListOfCauses(e);
        // check if any of them have been seen before
        for(Throwable child : listOfCauses) {
            if (seenCauses.contains(child)) {
                // already seen this outer Throwable so skip
                e = new RuntimeException("Duplicate found in causal chain so cropping to prevent loop ...");
                continue;
            }
            seenCauses.add(child);
        }

So, say the chain throws e1 and your code in onError throws e2 while being "somehow" a good citizen and remembers the original e1 as cause. e2 will then completely get evicted / replaced with a new RuntimeException telling us "Duplicate found in causal chain so cropping to prevent loop ...". If you then work with Crashlytics, you will not see anything beside this very runtime exception (even without the exception message, since this is not part of Crashlytics' output for causes for whatever reason). And you will not see anything on logcat, because the exception chain is already way too long.

— You are receiving this because you modified the open/close state. Reply to this email directly or view it on GitHub https://github.com/ReactiveX/RxAndroid/issues/21#issuecomment-221037240

JakeWharton avatar May 23 '16 17:05 JakeWharton

Using rxjava 1.1.1 - given this:

public class Main {
    public static void main(String[] args) {
        Observable
                .error(new IllegalArgumentException("illegal argument"))
                .subscribe(
                        success -> {},
                        error -> {
                            throw new IllegalStateException("illegal state exception", error); // Main.java, line 13
                        }
                );
    }
}

I see this:

Exception in thread "main" rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
    at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:192)
    at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
    at rx.Observable$ThrowObservable$1.call(Observable.java:10200)
    at rx.Observable$ThrowObservable$1.call(Observable.java:10190)
    at rx.Observable.subscribe(Observable.java:8407)
    at rx.Observable.subscribe(Observable.java:8374)
    at rx.Observable.subscribe(Observable.java:8178)
    at biz.thomaskeller.rxandroid_21.Main.main(Main.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 
    ... 13 more
Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
    at rx.exceptions.CompositeException.getCause(CompositeException.java:96)
    at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:707)
    at java.lang.Throwable.printStackTrace(Throwable.java:667)
    at java.lang.Throwable.printStackTrace(Throwable.java:643)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1061)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1052)
    at java.lang.Thread.dispatchUncaughtException(Thread.java:1952)
Caused by: java.lang.IllegalArgumentException: illegal argument
    at biz.thomaskeller.rxandroid_21.Main.main(Main.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.RuntimeException: Duplicate found in causal chain so cropping to prevent loop ...
    at rx.exceptions.CompositeException.getCause(CompositeException.java:112)
    at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:707)
    at java.lang.Throwable.printStackTrace(Throwable.java:667)
    at java.lang.Throwable.printStackTrace(Throwable.java:643)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1061)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1052)
    at java.lang.Thread.dispatchUncaughtException(Thread.java:1952)

whereas given this:

public class Main {
    public static void main(String[] args) {
        Observable
                .error(new IllegalArgumentException("illegal argument"))
                .subscribe(
                        success -> {},
                        error -> {
                            throw new IllegalStateException("illegal state exception");  // Main.java, line 13
                        }
                );
    }
}

I see this:

Exception in thread "main" rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
    at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:192)
    at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
    at rx.Observable$ThrowObservable$1.call(Observable.java:10200)
    at rx.Observable$ThrowObservable$1.call(Observable.java:10190)
    at rx.Observable.subscribe(Observable.java:8407)
    at rx.Observable.subscribe(Observable.java:8374)
    at rx.Observable.subscribe(Observable.java:8178)
    at biz.thomaskeller.rxandroid_21.Main.main(Main.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 
    ... 13 more
Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
    at rx.exceptions.CompositeException.getCause(CompositeException.java:96)
    at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:707)
    at java.lang.Throwable.printStackTrace(Throwable.java:667)
    at java.lang.Throwable.printStackTrace(Throwable.java:643)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1061)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1052)
    at java.lang.Thread.dispatchUncaughtException(Thread.java:1952)
Caused by: java.lang.IllegalArgumentException: illegal argument
    at biz.thomaskeller.rxandroid_21.Main.main(Main.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.IllegalStateException: illegal state exception
    at biz.thomaskeller.rxandroid_21.Main.lambda$main$1(Main.java:13)
    at biz.thomaskeller.rxandroid_21.Main$$Lambda$2/664223387.call(Unknown Source)
    at rx.Observable$28.onError(Observable.java:8187)
    at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157)
    at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
    at rx.Observable$ThrowObservable$1.call(Observable.java:10200)
    at rx.Observable$ThrowObservable$1.call(Observable.java:10190)
    at rx.Observable.subscribe(Observable.java:8407)
    at rx.Observable.subscribe(Observable.java:8374)
    at rx.Observable.subscribe(Observable.java:8178)
    at biz.thomaskeller.rxandroid_21.Main.main(Main.java:10)
    ... 5 more

realdadfish avatar May 23 '16 17:05 realdadfish

Hrm... but there seems to be more - because none of my actual crashes point me really at CompositeException.java:112, where the actual cause is replaced with the pseudo cause that crops the chain, still, I always only see the "upstream" exception.

realdadfish avatar May 23 '16 18:05 realdadfish

As a note, this seems to be fixed in https://github.com/ReactiveX/RxJava/pull/3977 and included in RxJava version 1.1.6.

paramsen avatar Feb 10 '17 16:02 paramsen

compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0.1'

AnnKrepchenko avatar Sep 22 '17 14:09 AnnKrepchenko