okhttp
okhttp copied to clipboard
Android P has more socket timeouts
OKHttp version - 3.4.2 on Android Some time our client is facing timeout exception.
Server is available, that we verified using some other service. Problem only come for post request, Timeout increased to 200 seconds still problem exists.
We debug on server as well and noticed that request not reaching to server, network connectivity is not issue as some other service with get request works well at same time.
- As soon as user switch from mobile network to wifi then all well.
Client code - OkHttpClient client = new OkHttpClient.Builder().connectTimeout(200, TimeUnit.SECONDS). readTimeout(200, TimeUnit.SECONDS) .writeTimeout(200, TimeUnit.SECONDS) .followRedirects(true) .followSslRedirects(true) .build();
Exception -
Caused by: java.net.SocketTimeoutException: timeout at okio.Okio$4.newTimeoutException(Okio.java:227) at okio.AsyncTimeout.exit(AsyncTimeout.java:284) at okio.AsyncTimeout$2.read(AsyncTimeout.java:240) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314) at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210) at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:186) at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:53) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170) at okhttp3.RealCall.execute(RealCall.java:60) at com.mexel.prx.util.general.HttpUtils.postInternal(HttpUtils.java:329) ... 8 more Caused by: java.net.SocketException: Socket closed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:151) at java.net.SocketInputStream.read(SocketInputStream.java:120) at okio.Okio$2.read(Okio.java:138) at okio.AsyncTimeout$2.read(AsyncTimeout.java:236)
same issues , use okhttp 3.7.0
ok will upgrade and verify
maybe the server problem
as soon as connected to wifi it works, so server is not a issue, it comes randomly however when problem start coming then even after trying 100 times it get timeout but as soon as connectivity changes or mobile reboot then it works. i will try with latest version and check
Even if the server or network is causing issues, this Exception should be handled somewhere and passed to listeners instead of crashing the whole thread. I've seen many people suggest to increase the timeouts as a fix, but it's only a workaround. I want my requests to time out in a reasonable amount of time, but I want to be able to handle the error.
Stack traces of related timeout errors:
Fatal Exception: java.net.SocketTimeoutException: failed to connect to allaboutsamsung.de/104.28.16.76 (port 443) from /W.X.Y.Z (port 34860) after 10000ms
at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
at libcore.io.IoBridge.connect(IoBridge.java:130)
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:616)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:70)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:238)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:158)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
and
Fatal Exception: java.net.SocketTimeoutException: timeout
at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.java:593)
at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:601)
at okhttp3.internal.http2.Http2Stream.takeResponseHeaders(Http2Stream.java:146)
at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:125)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
OkHttp notifies you with an exception on execute()
calls, and a callback on enqueue()
calls. It doesn't crash the whole thread.
@swankjesse That's strange. I'm pretty sure I'm catching this type of Exception everywhere and yet I receive crash reports like the ones above. I've spent several hours on making sure I'm handling it everywhere, but maybe I'm still missing a catch
somewhere. The stack trace unfortunately makes it hard to tell since I can't see where the request was started.
These are async calls. You're handling them in onFailure?
I use OkHttp in two ways: Directly with coroutines and using Retrofit's Deferred
-adapter.
Direct usage works like this:
override fun onFailure(call: Call, e: IOException) {
// Don't bother with resuming the continuation if it is already cancelled.
if (continuation.isCancelled) return
continuation.resumeWithException(e)
}
I suppose Retrofit does it similarly. In both ways I then just use try-catch as I would in plain old procedural code, handling any IOException
.
Thanks for your help!
Not sure but is it linked with any static variable declare some where. Because in our case we do have some large file upload in another thread and that gets timeout sometime.
As soon as we switch of net and restart in 5-10 minutes then everything start working fine.
Harish
On Fri, May 4, 2018, 20:36 Yuri Schimke [email protected] wrote:
We should handle this, it's not an IOException
public class SocketTimeoutException extends java.io.InterruptedIOException {
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/square/okhttp/issues/3974#issuecomment-386630482, or mute the thread https://github.com/notifications/unsubscribe-auth/AkvlmEOwvf7PegLWrT5_8TYwMaa_3LxGks5tvG6DgaJpZM4Tbo6O .
okhttp-3.8.0 same problem:
.onFailure(UploadIntentService.java:72) timeout java.net.SocketTimeoutException: timeout at okio.Okio$3.newTimeoutException(Okio.java:210) at okio.AsyncTimeout.exit(AsyncTimeout.java:277) at okio.AsyncTimeout$2.read(AsyncTimeout.java:244) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314) at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210) at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:75) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at java.lang.reflect.Method.invoke(Native Method) at com.android.tools.profiler.support.network.okhttp.reflection.okhttp3.Interceptor$$Chain$.proceed(Interceptor$.java:52) at com.android.tools.profiler.support.network.okhttp.OkHttp3Interceptor.invoke(OkHttp3Interceptor.java:53) at java.lang.reflect.Proxy.invoke(Proxy.java:393) at $Proxy1.intercept(Unknown Source) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)
the same in 3.10.0
@swankjesse This is the Socket Timeout Error which in my monitor data. It happend about 200000+/day. Some time it will reach to 500000+/day. I'm sure the server may don't have any problem. It always happend with HTTP/1.1 And HTTP/2.0
@swankjesse okhttp 3.10.0
Hello, I find two problems here.
It looks like problem 1 caused by problem 2.
This is the Socket Timeout Error which in my monitor data. It happend about 400000.
I hope to receive your feedback
thanks!
Bug 1:
at okio.Okio$4.newTimeoutException(Okio.java:232)
at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
at okio.RealBufferedSource.read(RealBufferedSource.java:47)
at okhttp3.internal.http1.Http1Codec$AbstractSource.read(Http1Codec.java:363)
at okhttp3.internal.http1.Http1Codec$UnknownLengthSource.read(Http1Codec.java:507)
at okio.Buffer.writeAll(Buffer.java:1053)
at okio.RealBufferedSource.readString(RealBufferedSource.java:200)
at okhttp3.ResponseBody.string(ResponseBody.java:175)
at com.jd.JDResponse.
Bug 2: okio.RealBufferedSource.rangeEquals (RealBufferedSource.java:377) okio.RealBufferedSource.rangeEquals (RealBufferedSource.java:371) okhttp3.internal.Util.bomAwareCharset (Util.java:397) okhttp3.ResponseBody.string (ResponseBody.java:173) com.jd.JDResponse. (JDResponse.java:23) com.jd.net.core.HttpCore$2.onResponse (HttpCore.java:175) okhttp3.RealCall$AsyncCall.execute (RealCall.java:135) okhttp3.internal.NamedRunnable.run (NamedRunnable.java:32) java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112) java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587) java.lang.Thread.run (Thread.java:818)
That's great I hope team will be able to fix this quick
Regards Harish
On Mon, Jun 11, 2018, 18:33 jacky_z [email protected] wrote:
@swankjesse https://github.com/swankjesse okhttp 3.10.0 Hello, I find two problems here. It looks like problem 1 caused by problem 2. This is the Socket Timeout Error which in my monitor data. It happend about 400000. I hope to receive your feedback thanks! Bug 1: at okio.Okio$4.newTimeoutException(Okio.java:232) at okio.AsyncTimeout.exit(AsyncTimeout.java:285) at okio.AsyncTimeout$2.read(AsyncTimeout.java:241) at okio.RealBufferedSource.read(RealBufferedSource.java:47) at okhttp3.internal.http1.Http1Codec$AbstractSource.read(Http1Codec.java:363) at okhttp3.internal.http1.Http1Codec$UnknownLengthSource.read(Http1Codec.java:507) at okio.Buffer.writeAll(Buffer.java:1053) at okio.RealBufferedSource.readString(RealBufferedSource.java:200) at okhttp3.ResponseBody.string(ResponseBody.java:175) at com.jd.JDResponse.(JDResponse.java:33) at com.jd..net.core.HttpCore$2.onResponse(HttpCore.java:188) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) Caused by: java.net.SocketException: Socket closed at java.net.SocketInputStream.read(SocketInputStream.java:203) at java.net.SocketInputStream.read(SocketInputStream.java:139) at okio.Okio$2.read(Okio.java:140) at okio.AsyncTimeout$2.read(AsyncTimeout.java:237) ... 13 more
Bug 2: okio.RealBufferedSource.rangeEquals (RealBufferedSource.java:377) okio.RealBufferedSource.rangeEquals (RealBufferedSource.java:371) okhttp3.internal.Util.bomAwareCharset (Util.java:397) okhttp3.ResponseBody.string (ResponseBody.java:173) com.jd.JDResponse. (JDResponse.java:23) com.jd.net.core.HttpCore$2.onResponse (HttpCore.java:175) okhttp3.RealCall$AsyncCall.execute (RealCall.java:135) okhttp3.internal.NamedRunnable.run (NamedRunnable.java:32) java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112) java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587) java.lang.Thread.run (Thread.java:818)
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/square/okhttp/issues/3974#issuecomment-396235227, or mute the thread https://github.com/notifications/unsubscribe-auth/AkvlmOQBeOIDKNYS6kWALJ3cNYNpiR30ks5t7mqvgaJpZM4Tbo6O .
I have same issue only on Android 7 & 8 okhttp 3.10.0. After wifi restart or connect to the mobile data problem is resolved by itself.
same issue
sounds similar to my issue: I often see timeout errors after unlocking my screen.
if I have my app open, turn off the screen, stick phone in pocket for a couple of minutes, take phone out of pocket and turn on screen, my app triggers a couple of net requests during onResume, I then often but not always see a few timeouts.
FYI @swankjesse: https://stackoverflow.com/questions/41439830/android-7-1-1-nougat-app-standby-and-network
Any update or workaround solution available for this issue?
My server uses nginx 1.10.0 and supports HTTP2. I disabled HTTP2 in OkHttp and for now app is working. I'm not sure but maybe it is solution for me:
okHttpBuilder.protocols(Collections.singletonList(Protocol.HTTP_1_1));
@putuyuwono @steelbytes @hkachoria @7280423 @lizhangqu @cbruegg @Warpath
which nginx version do you use? Maybe related to https://github.com/square/okhttp/issues/2543
@askarsyzdykov both HTTP/1.1 and HTTP/2.0 have this problem. I have do an A/B Test for this. the timeout exception don't seem to change.
I met the same problem : java.net. SocketTimeoutException, version:3.9.1
I'm having this problem too.. When I switch networks (cellular to WIFI) it works for a while (sometimes some minutes, sometimes for hours) then starts giving the TimeoutException.. Also if I open Chrome and make the GET request I get the answer immediately without problem but if I switch back to my app using OkHttp I keep getting timeouts...
I have a similar error. Restarting a device fixes the problem. It usually happens when I execute multi-threaded downloads.
java.net.SocketTimeoutException: timeout com.android ettimeoutexception: timeoutcom.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:221) com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:261) com.android. .okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:215) com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:60) com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:466) com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:396)
I've found a way to reproduce this, though I'm not entirely sure that this is what our customers are hitting. I've been able to trigger this on Android 7.1.2, and Android P r3 using these steps:
https://developer.android.com/training/monitoring-device-state/doze-standby
adb shell dumpsys deviceidle force-idle
This puts your device into deep sleep / Doze mode. Network access is limited in this condition.
adb shell dumpsys deviceidle unforce
This wakes your device up, and network access is no longer expected to be limited.
Launch your app now, and I get the socket timeouts. I would point out that this doesn't seem to be 100% of how to reproduce this, as I couldn't get this to work across all my test devices.
Interestingly, we do check with the ConnectivityManager
to ensure we have a network available before issuing the call, so it does think there is something available. I checked with a debugger, and while debugging I could never reproduce it, but the connectivity manager always reported an active connection to my wifi.
On a Samsung running android 8.0.0 I was unable to reproduce this using these steps atll. Even if I never issue the unforce
command things work. I do see users hitting this on Android 8.0 though. I also tried setting my app inactive on Android 8 with no luck.
@inktomi I reproduce the problem in your way.
Definatelly an issue here. I came to similar conclusions as @inktomi , before I even found this thread.