6

I have an RxJava chain request that should release some locks onError(), or onComplete(), so, basically, what my problem is: when I set read, connect and write timeouts to my OkHttpClient, I don't get the desired behavior. I'm using Retrofit2 and OkHttp3.6.0 Here's my simplified client:

OkHttpClient.Builder builder = new OkHttpClient.Builder()
           .readTimeout(30, TimeUnit.SECONDS)
           .connectTimeout(30, TimeUnit.SECONDS)
           .writeTimeout(30, TimeUnit.SECONDS)
OkHttpClient okHttpClient = builder.build();

Here's a simplified version of the chain request I have:

public <T extends Response> Observable<T> doSomething(Observable<T> base) {
    isLocked = true;
    return someApiCall()
               .flatMap(apiResponse -> handleResponse(apiResponse, base)
                   .doOnError(throwable -> {
                       isLocked = false;
                   })
                   .doOnCompleted(() -> {
                       isLocked = false;
                   }));
}

handleResponse() makes another API call and returns an Observable<Response<Something>> but, as I've said, it sometimes fails with a HTTP FAILED: java.net.SocketException: Socket closed and it never finishes the Observable, so, onError() or onComplete() are never called. I've tried onTerminate() also, but with no luck. When I remove the timeout settings from the OkHttlClient, the SocketException is actually thrown and caught which releases the isLocked variable. I've tried wrapping the handleResponse() return statement with a try {} catch (Exception e) {} block, but even that doesn't catch the SocketException when the custom timeouts are set. Any ideas?

6
  • does the handleResponse do Retrofit call as well? what do you define as "finish the observable"? any how the problem might be as it's seems you're applying the doOnError/Completed just on the inner flatMapped called, thus - the someApiCall() can fail and you will not catch it Commented Feb 20, 2017 at 5:26
  • yes it does do a Retrofit call. As I mentioned it's a simplified chain, the real one should be originalApiCall (fails) -> apiCallOne -> apiCallTwo -> originalApiCall (it's a session renewal chain) I've tried adding both doOnError() and doOnTerminate to the outer call, but with no luck. The problem is that all other exceptions are caught in the chain, except for the Socket closed Commented Feb 20, 2017 at 12:32
  • ok, I see, so what do happen with the SocketException? how do you know that's the exepction? Commented Feb 20, 2017 at 12:36
  • I just get a one line entry in the logcat output that says exactly that: "HTTP FAILED: java.net.SocketException: Socket closed" and it always happens, it's not a false positive. Commented Feb 20, 2017 at 12:41
  • can you attach logcat ? Commented Feb 20, 2017 at 13:15

3 Answers 3

4

I solved this situation in Android with the option retryOnConnectionFailure(true) set with the builder.

Given that Retrofit 2 just receive the OkHttpClient object.

Sign up to request clarification or add additional context in comments.

2 Comments

You sir, made my day today
Are there any downsides to this?
1

In Retrofit 2, exceptions below/above the HTTP layer are not reported as Response<?>, but as actual exceptions via onError.

In your case, you should move the doOnError/doOnCompleted out of the flatMap; as it is right now they respond to errors generated in the handleResponse only - but that method won't be called if someApiCall returns an error Observable.

4 Comments

I'm aware of that and I've tried adding the doOnError and doOnCompleted to the outer call too, but with no luck, even doOnTerminate and doAfterTerminate, which are never reached (in case of the Socket exception). The real issue is that all of the other exceptions are caught by the chain, except for the Socket closed, which seems to be handled somewhere else...
Can you wrap the call in a defer? Like Observable.defer(()-> someApiCall())
Since I'm fairly new to RxJava, could you please tell me what good will this do? I'll try it out, but I don't know what to expect.
defer will postpone calling the someApiCall() method until the Observable is subscribed to. The idea is that you're doing something synchronously in that call that causes this exception; by defer-ing you sould catch that in the subscription.
1

It turns out that the request was getting unsubscribed, which caused the appearance of the HTTP FAILED: java.net.SocketException: Socket closed error, so, the solution was just adding this code to the chain:

.doOnUnsubscribe(() -> {
    isLocked = false;
})

I still don't understand why the custom timeout value was triggering the SocketException, but I suppose that the socket was waiting for the timeout to expire and close, or complete the request and close, but it got interrupted by the unsubscribe call and thus closed unexpectedly.

1 Comment

So what's causing the "Socket closed"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.