retrofit: Random throw java.io.InterruptedIOException when use OkHttpClient on Android
I have set up a RestAdpter with OKHttpClient:
RestAdapter.Builder builder = new RestAdapter.Builder()
.setClient(new OkClient(okHttpClient))
// .setClient(new AndroidApacheClient())
.setConverter(converter)
.setEndpoint(API_ENDPOINT);
When I tried to make some calls, sometimes Retrofit throw a java.io.InterruptedIOException:
java.io.InterruptedIOException: thread interrupted
at okio.Timeout.throwIfReached(Timeout.java:145)
at okio.Okio$1.write(Okio.java:75)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at okio.RealBufferedSink.flush(RealBufferedSink.java:221)
at com.squareup.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
at com.squareup.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:915)
at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:95)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:902)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:760)
at com.squareup.okhttp.Call.getResponse(Call.java:274)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
at com.squareup.okhttp.Call.execute(Call.java:81)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
at retrofit.RxSupport$2.run(RxSupport.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:856)
I used Observable to subscribe for the result but no Error is thrown back to subscriber.
And It doesn’t happen when I use AndroidApacheClient.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 22 (2 by maintainers)
As I understand now, RxJava for some reason interrupts thread on unsubscription. So if you unsubscribe during request execution, errors like this can appear.
This is a write timeout which defaults to 15 seconds. I have no idea what Apache’s defaults to, but it’s probably higher, or infinite. You can call
setWriteTimeouton theokHttpClientinstance to set this to something larger.I was also facing random
java.io.InterruptedIOException: thread interruptedexceptions, caused bywaitForIoorthrowIfReached. At last, I found that I was not usingsubscribeOnto make sure the call happen on threads other than main. After addingsubscribeOn(Schedulers.io())the issue was gone.@konmik what exactly was the RxJava hook you have used? I am now using RxJava2 bu I am receiving this error randomly on some of my user devices. Its really annoying to have your app crashes.
(It’s also not a Retrofit problem but an HTTP client configuration issue)