21

I've an Android app with multiple observers of type A that subscribe with several Observables of type B. The subscription is done in IO Scheduler and the observation on the Android main thread.

The problem that I've is that randomlly after some work one message emitted by B is never received in A and after some hours of debbuging I can't find the cause.

Relevant code when the problem happens:

"NEXT1" and "NEXT2" are printed but "RECEIVED","ERROR", COMPLETED aren't.

            //The subscription
            B.getMessate()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(A);

            //B
            Observable<msg> getMessage() {
                 return Observable.create(new Observable.OnSubscribe<msg>() {    
                      public void call(Subscriber<? super msg> subscriber) {
                         ...
                         subscriber.onNext(msg)
                         println("NEXT1")
                      }
                 }).doOnNext({ (o) -> println("NEXT2")});
            }



            //A 
            onNext(msg) {
                  //Never called when problem happens
                  println("RECEIVED")
            }
            onError(msg) {
                  //Never called when problem happens
                  println("ERROR")
            }
            onError(msg) {
                  //Never called when problem happens
                  println("COMPLETED")
            }

Anyone has some clue? or any recommendation for debugging that?

What I've checked:

  • I've paused the app and checked that all threads to see if one is locked. And aparentlly all working threads are parked and main thread is waiting messages in android message queue.
  • Observers never call unsubscribe()
lujop
  • 13,504
  • 9
  • 62
  • 95
  • I'm assuming this is just pseudo code and that you're not directly calling `onNext(msg)` in your `call` method but actually doing `subscribe.onNext(msg)`? – Miguel Aug 15 '14 at 20:23
  • Why don't you give us a real short piece of code that can reproduce your issue. It might make it easier to spot the problem. – Miguel Aug 15 '14 at 20:34
  • I've corrected pseudocode calling onNext on the subscriber. About giving real short piece of code the problem is that real code that comes before subscriber.onNext() is big and complex. But I think it's irrelevant because "NEXT2" is called. As always the devil is on de details but that code it's my best intent to illustrate my problem in a clear way. – lujop Aug 15 '14 at 21:02

1 Answers1

24

By now I can't reproduce the problem but I've found RxJavaDebug a very good tool for debugging.

It's use is simple: add the library as a dependency and at application start register a listener:

  RxJavaPlugins.getInstance().registerObservableExecutionHook(new DebugHook(new DebugNotificationListener() {
      public Object onNext(DebugNotification n) {
          Log.v(TAG, "onNext on " + n);
          return super.onNext(n);
      }


        public Object start(DebugNotification n) {
            Log.v(TAG, "start on " + n);
            return super.start(n);
        }


        public void complete(Object context) {
            Log.v(TAG, "complete on " + context);
        }

        public void error(Object context, Throwable e) {
            Log.e(TAG, "error on " + context);
        }
  }));

That will log messages while they go between observables and operators.

Steffen Funke
  • 2,168
  • 1
  • 21
  • 18
lujop
  • 13,504
  • 9
  • 62
  • 95