2

I'm using Retrofit2 and RxJava2CallAdapter and I have an issue making the difference between a request canceled by the client side and another one canceled by the server side.

If my user leave the current screen where a request has been started, the request is canceled which is the expected behavior.

But, sometimes I get :

HTTP FAILED: java.io.IOException: Canceled or HTTP FAILED: java.net.SocketException: Socket closed

if the server cancel the request (really rare and hard to reproduce), but those errors are not transmit to the DisposableObserver. If I look in the code of CallExecuteObservable, I can see that if the request is canceled, nothing is triggered in the DisposableObserver (neither onError(), neither onComplete()).

So my question is: Is it possible to make a difference and/or to handle this case ? Thanks in advance.

Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57
  • Where do you see the exception? Are you sure the server close the connection, and that's what caused the exception ? When you cancel the request, you will not get onError. – yosriz Apr 12 '17 at 15:47
  • I see the exception on the log using HttpLoggingInterceptor. I don't really know what causing the exception, since nothing else happen and I'm not canceling the request myself, I assumed the request is canceled on the server side. – Paul-Marie Tetedoie Apr 12 '17 at 17:27
  • the point is that unsubscribing (i.e. dispose) will not trigger onError, so this exception are probably come from canceling when user leave the screen. (which can be seen with logging interceptor) so be sure you check it (maybe post some code), any other error should (server closed the connection) should trigger onError, why are you sure you have a problem at server side anyway? – yosriz Apr 12 '17 at 19:16
  • Yes, I know that unsubscribing/disposing will not trigger anything. But the thing is that I did not dispose/cancel anything. Just sometimes I get those two exceptions which didn't trigger anything. I also get some SocketTimeoutException sometime but they trigger onError. – Paul-Marie Tetedoie Apr 12 '17 at 20:04
  • I'm not sure the problem is on the server side, I just assume it is, because I don't do anything different on the client, it just starting the request as usual and stay stock on loading because none of the Observer method is called. – Paul-Marie Tetedoie Apr 12 '17 at 20:07
  • I'm not sure posting some code will help since it's really hard to reproduce (it happen like 0.1% of the time maybe even less) and in addition the api isn't a public api. – Paul-Marie Tetedoie Apr 12 '17 at 20:12
  • https://stackoverflow.com/questions/40823134/retrofit-api-call-receives-http-failed-java-io-ioexception-canceled – hmac Aug 25 '17 at 11:04

1 Answers1

0

What helped to me is to replace:

implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

with:

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'

This helped me with this issue and I get onError() called everytime. More info on this: https://github.com/JakeWharton/retrofit2-rxjava2-adapter

Komoi
  • 43
  • 8