Okhttp: Android P has more socket timeouts

Created on 19 Apr 2018  ·  73Comments  ·  Source: square/okhttp

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)

android

Most helpful comment

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.

All 73 comments

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 notifications@github.com 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

data

@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)

That's great I hope team will be able to fix this quick

Regards
Harish

On Mon, Jun 11, 2018, 18:33 jacky_z notifications@github.com 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.

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.

Workaround

Configure the app’s battery settings to be “not optimized”. (source)

Definatelly an issue here. I came to similar conclusions as @inktomi , before I even found this thread.

Is there any other solution for this issue is getting worst in recent days, any alernative for okhttp?

Tracking this issue.. I strongly believe Doze mode to be the cause of this all. It happens to me in the background, only a few requests make it through and then nothing.

Tracking this issue. Occuring rarely (I didn't have luck in reproducing it, in none of mentioned ways), but still irritating that we cannot even catch it (not mentioning solving the issue).
My users do have this issue from Android 4 to Android 8, so I am not sure about the Doze mode, but maybe many factors come in play?

I also noticed this issue when using Samsung Android s6 edge 5.1.1.
and that exception occurs when wifi state change (on -> off -> on).
this case is for http/1.1 and when using http/2.0 it happens as in #3146.
i think this issue is due to dead clients remaining in the connection pool when Wi-Fi is turned off (in specific models).
comments in #3146 shows how solve this issue, and this has worked for me.

OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
            .connectTimeout(connectTimeoutMillis, TimeUnit.MILLISECONDS)
            .retryOnConnectionFailure(true)
            .readTimeout(readTimeoutMillis, TimeUnit.MILLISECONDS).connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS));

Similar issue happens only in India, don't see the issue in US.

Caused by: java.net.SocketTimeoutException: timeout
at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:221)
at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:250)
at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:217)
at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:316)
at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:310)
at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:206)
at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:385)
at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:144)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:917)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:789)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:499)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:435)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseMessage(HttpURLConnectionImpl.java:562)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseMessage(DelegatingHttpsURLConnection.java:109)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseMessage(Unknown Source:0)

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.

Have you solved it now? How to solve it?

@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

data

你好,请问你解决了吗?

Any guidance on a change we’d make to make you happy?

@swankjesse Any fixes for this? or Downgrading to HTTP-1 will resolve the issue?
We are also getting this issues for the Samsung Galaxy Note8 and Note9

@swankjesse Any fixes for this? or Downgrading to HTTP-1 will resolve the issue?
We are also getting this issues for the Samsung Galaxy Note8 and Note9

Downgrading to HTTP 1 helped us.

Ok, funny thing. I've encountered on this issue while working with Ricoh Theta V. Where Android Pie did A LOT of issues. Since connection is P2P via internal camera WiFi. Overall some calls go through, some are causing socket timeout. Only 100% working workaround for timeout was to disable mobile data. Maybe this will help someone

How to fix this issue? Or only Downgrading to HTTP-1 will resolve the issue?
I also getting this issues in Android 9(Pie)

@AditMulyadi if you are binding network by using its socket factory, than remove that line of code. Downgrade to HTTP-1 didn't helped me

I have replaced okhttp with cronet which is used in chromium, and the network error has reduced to less than half of okhttp.

I met the same problem : java.net. SocketTimeoutException,
version:3.9.1

I met the same prolem and the version is also same

No solution till now, we are planning to move to cronet as suggested to see if issue is with okhttp.

Same issue. Experienced more with Samsung Galaxy phones. Moved to Cronet and error has dropped significantly.

I am also facing same issue again and again. I have tried increasing the timeout time also but it does not help. Tried retryonConnectionFailure also.
E/Error: java.net.SocketTimeoutException: timeout
java.net.SocketTimeoutException: timeout
at okio.Okio$4.newTimeoutException(Okio.java:230)
at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:211)
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.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)
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:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:144)
at okio.Okio$2.read(Okio.java:139)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)

I am using Andorid P and OKHTTP 3.8.1

We are seeing this SocketTimeoutException across a variety of devices and is affecting our crash rates. Any discussion on if this will be addressed?

@swankjesse We're seeing this as well, particularly on samsung devices. Is this on your radar at all to fix?

@ScottPierce True, Samsung devices are more prone to this issue, but you can see it on Pixel devices as well. I've tested it also on Nokia phones and same result. This is not happening on any device with Android 8 or less

You guys should take a look on this website:
https://dontkillmyapp.com/
Root cause of this issue is all about Android OEMs

Using java.net.URL works for me for downloading big files.

same issues on okhttp 4.2.0

I am also facing same issue again and again. I have tried increasing the timeout time also but it does not help. Tried retryonConnectionFailure also.
E/Error: java.net.SocketTimeoutException: timeout
java.net.SocketTimeoutException: timeout
at okio.Okio$4.newTimeoutException(Okio.java:230)
at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:211)
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.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)
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:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:144)
at okio.Okio$2.read(Okio.java:139)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)

I am using Andorid P and OKHTTP 3.8.1

same stack error +1, Okhttp 3.12.3
I also get the timeout with the stack:
Http2Stream.takeHeaders()
Http2Codec.readResponseHeaders()

Both Http2 and Http1.1 get lots of the timeout error when readResponseHeaders.

I'm seeing this behavior on Android 10 with OkHttp 4.0.1 and 4.2.0. I haven't dug into it further, but I noticed that the SocketTimeoutException always immediately follows a system-initiated Wi-Fi scan as can be seen by registering WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.

Here's the stats of this issue from my app -

image

The particular API call is scheduled periodically using WorkManager. My guess is that the worker is run incorrectly while the device is still in Doze mode hence disallowing any network operations.

I think the most severe consequence is fixed in OkHttp 4.3. That fix is also backported to 3.14.6 and 3.12.8.

Next steps –

  • [ ] Reproduce following instructions in this comment.
  • [ ] Track Android network changes and clear the connection pool in response

@swankjesse I've got code for this Android hook on GitHub. Are you actively working on this or do you want me to put up a PR?

Put up a PR please!

BTW the code I have looks at both (background/foreground events) and per network events (e.g. wifi available, cell connection is scheduled to stop in 30 seconds).

The optimal result is probably some combination of these. But if you look at this comment (https://github.com/square/okhttp/issues/3974#issuecomment-552306067) note it's Android 9 and only in background. So that would be the minimal fix (version >= 9 && backgrounded && app not allowed background connectivity).

So should I start off with a broad fix enabled by registering the Context (so we can grab services) with the Android Platform? This would be an android specific API inside a new module.

Is the ultimate decision this thing makes to choose connections from the pool? I think I'm overdue on my promise to offer pluggable policies on the connection pool, which potentially ties into this also.

The optimal result is probably some combination of these. But if you look at this comment (#3974 (comment)) note it's Android 9 and only in background. So that would be the minimal fix (version >= 9 && backgrounded && app not allowed background connectivity).

@yschimke Majority of it is on Android 9 but this is affecting almost all Android versions.
image
I've got about 150M non-fatals in the last 7 days.

Not IIUC. My understanding is that on Android P, your app probably can't do IO when in the background. But things might recover when the app is foregrounded again. I can start with this and see if it solves this specific problems.

As well as this Android exposes events for each network and controls these based on it's own policies. So you can also model networks (id, name, local ip address, per network proxies, availability events, cost). You could just switch networks so new streams don't reuse a connection that Android wants to reclaim, you use the preferred network. Or allow users to send corp requests only over Wifi. Background Video requests only when it's really Wifi etc.

I think ultimately having visibility of what is going on will also allow us to understand and model it correctly, allow developers to make better decisions of how to use the network and when.

@prempalsingh Thanks, that's useful. I guess than I'm really talking about the word more in the issue description.

Seem like facebook use okhttp too so I would like to drop my log here

03-03 20:59:39.722 17134 17323 W System.err: java.net.SocketTimeoutException: failed to connect to www.facebook.com/2a03:2880:f12a:83:face:b00c:0:25de (port 443) from /2403:6200:89a6:e4c6:b074:9bd5:ad6f:be67 (port 44994) after 30000ms
03-03 20:59:39.723 17134 17323 W System.err:    at libcore.io.IoBridge.connectErrno(IoBridge.java:191)
03-03 20:59:39.723 17134 17323 W System.err:    at libcore.io.IoBridge.connect(IoBridge.java:135)
03-03 20:59:39.723 17134 17323 W System.err:    at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
03-03 20:59:39.723 17134 17323 W System.err:    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
03-03 20:59:39.723 17134 17323 W System.err:    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
03-03 20:59:39.723 17134 17323 W System.err:    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
03-03 20:59:39.723 17134 17323 W System.err:    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
03-03 20:59:39.723 17134 17323 W System.err:    at java.net.Socket.connect(Socket.java:621)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.Platform.connectSocket(Platform.java:182)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:145)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
03-03 20:59:39.723 17134 17323 W System.err:    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-03 20:59:39.724 17134 17323 W System.err:    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
03-03 20:59:39.724 17134 17323 W System.err:    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
03-03 20:59:39.724 17134 17323 W System.err:    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
03-03 20:59:39.724 17134 17323 W System.err:    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
03-03 20:59:39.724 17134 17323 W System.err:    at com.facebook.ads.redexgen.X.3v.A01(:5348)
03-03 20:59:39.724 17134 17323 W System.err:    at com.facebook.ads.redexgen.X.3v.A0I(:5524)
03-03 20:59:39.724 17134 17323 W System.err:    at com.facebook.ads.redexgen.X.4p.A00(:6930)
03-03 20:59:39.724 17134 17323 W System.err:    at com.facebook.ads.redexgen.X.4p.doInBackground(:6949)
03-03 20:59:39.724 17134 17323 W System.err:    at android.os.AsyncTask$3.call(AsyncTask.java:378)
03-03 20:59:39.724 17134 17323 W System.err:    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
03-03 20:59:39.724 17134 17323 W System.err:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
03-03 20:59:39.724 17134 17323 W System.err:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
03-03 20:59:39.724 17134 17323 W System.err:    at java.lang.Thread.run(Thread.java:919)
03-03 20:59:39.761  4165  4165 D NetworkController.WifiSignalController: Change in state from: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi 5 Icons),activityIn=true,activityOut=true,rssi=-48,lastModified=03-03 20:59:06,ssid="Komodo-2",wifiGenerationVersion=5,isReady=false,isTransient=false,statusLabel=null
03-03 20:59:39.761  4165  4165 D NetworkController.WifiSignalController:    to: connected=true,enabled=true,level=4,inetCondition=1,iconGroup=IconGroup(Wi-Fi 5 Icons),activityIn=false,activityOut=true,rssi=-48,lastModified=03-03 20:59:06,ssid="Komodo-2",wifiGenerationVersion=5,isReady=false,isTransient=false,statusLabel=null

@Thaina They use HttpsUrlConnection in this way, which use repackaged OkHttp that is part of newer Android core.

We also have this issue, we could reproduce it in the following devices:

  • Samsung A5 - Android 7
  • Samsung S5 - Android 7
  • Samsung Galaxy S2 - Android 7
  • Samsung S6 - Android 7
  • Samsung A3 - Android 8
  • Xiaomi Mi A1 - Android 9

Reproduction steps

Enable battery save mode.

  1. Open app and put it in background
  2. Open chrome (or any app using network) and use it for 1-2 minutes while making requests
  3. Restore app and click somewhere to trigger a network request
  4. SocketTimeoutException

java.net.SocketTimeoutException: failed to connect to xxx/2409:8c54:b000:701:1c5:991a:1136:b4a (port 443) from /2409:8962:50a:4b84:85b4:4615:39e8:211e (port 58414) after 10000ms

We also face the same problem, use chrome open the url it's ok, but okhttp always timeout.

Okhttp 4.9.0
OS: Android 9
Device: HUAWEI

@crossle @iliaskomp is it eventually getting back to a healthy state (expected) after a cluster of failed requests? Or is it staying unhealthy?

@crossle @iliaskomp is it eventually getting back to a healthy state (expected) after a cluster of failed requests? Or is it staying unhealthy?

It' back to healthy state.

java.net.SocketTimeoutException: failed to connect to xxx/2409:8c54:b000:701:1c5:991a:1136:b4a (port 443) from /2409:8962:50a:4b84:85b4:4615:39e8:211e (port 58414) after 10000ms

We also face the same problem, use chrome open the url it's ok, but okhttp always timeout.

Okhttp 4.9.0
OS: Android 9
Device: HUAWEI

Hello, could you please tell me if there is any conclusion about huawei mobile phone connection timeout? I also found similar feedback. All of them were Huawei phones, also java.net.SocketTimeoutException: failed to connect to.

Was this page helpful?
0 / 5 - 0 ratings