Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android P has more socket timeouts #3974

Closed
hkachoria opened this issue Apr 19, 2018 · 86 comments
Closed

Android P has more socket timeouts #3974

hkachoria opened this issue Apr 19, 2018 · 86 comments
Labels
android Relates to usage specifically on Android
Milestone

Comments

@hkachoria
Copy link

hkachoria commented Apr 19, 2018

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)

@Warpath
Copy link

Warpath commented Apr 26, 2018

same issues , use okhttp 3.7.0

@hkachoria
Copy link
Author

ok will upgrade and verify

@philipSZ
Copy link

maybe the server problem

@hkachoria
Copy link
Author

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

@cbruegg
Copy link

cbruegg commented May 4, 2018

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.

@cbruegg
Copy link

cbruegg commented May 4, 2018

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)

@swankjesse
Copy link
Collaborator

OkHttp notifies you with an exception on execute() calls, and a callback on enqueue() calls. It doesn't crash the whole thread.

@cbruegg
Copy link

cbruegg commented May 4, 2018

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

@swankjesse
Copy link
Collaborator

These are async calls. You're handling them in onFailure?

@cbruegg
Copy link

cbruegg commented May 4, 2018

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!

@yschimke yschimke added bug Bug in existing code and removed bug Bug in existing code labels May 4, 2018
@hkachoria
Copy link
Author

hkachoria commented May 5, 2018 via email

@louiewh
Copy link

louiewh commented May 17, 2018

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)

@lizhangqu
Copy link

the same in 3.10.0

@lizhangqu
Copy link

lizhangqu commented Jun 10, 2018

@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

@7280423
Copy link

7280423 commented Jun 11, 2018

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

@hkachoria
Copy link
Author

hkachoria commented Jun 11, 2018 via email

@askarsyzdykov
Copy link

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.

@gulzatique
Copy link

same issue

@steelbytes
Copy link

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.

@askarsyzdykov
Copy link

FYI @swankjesse: https://stackoverflow.com/questions/41439830/android-7-1-1-nougat-app-standby-and-network

@putuyuwono
Copy link

Any update or workaround solution available for this issue?

@askarsyzdykov
Copy link

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 #2543

@lizhangqu
Copy link

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

@Microhx
Copy link

Microhx commented Jun 26, 2018

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

@xradeon
Copy link

xradeon commented Jun 26, 2018

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...

@AndroidPat
Copy link

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)

@inktomi
Copy link

inktomi commented Jun 27, 2018

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.

@lizhangqu
Copy link

@inktomi I reproduce the problem in your way.

@yschimke
Copy link
Collaborator

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 (#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).

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.

@swankjesse
Copy link
Collaborator

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.

@prempalsingh
Copy link

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.

@yschimke
Copy link
Collaborator

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.

@yschimke
Copy link
Collaborator

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

@Thaina
Copy link

Thaina commented Mar 4, 2020

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

@arcao
Copy link

arcao commented Mar 17, 2020

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

@iliaskomp
Copy link

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

@crossle
Copy link

crossle commented Sep 15, 2020

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

@yschimke
Copy link
Collaborator

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

@crossle
Copy link

crossle commented Sep 16, 2020

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

@tanghb
Copy link

tanghb commented Dec 21, 2020

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.

@zomanubhav87
Copy link

zomanubhav87 commented Feb 3, 2021

Hi, I am still getting socket timeouts on Android 11, also timeouts are still present on older android versions.
This is happening most of the time when network call is triggered from work manager.
@swankjesse @JakeWharton Can you help in solving this?

@yin-haoran
Copy link

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.

hello, I have encountered the same problem. Do you have a conclusion? Thanks!

@hiepu
Copy link

hiepu commented Dec 7, 2021

  1. i also face this issue.
    it's happen 1000 times per day, with 10000 DAU android app.
Non-fatal Exception: java.net.SocketTimeoutException: failed to connect to myapp-api.net/222.158.xxx.xxx (port 443) from /172.18.xx.xxx (port 46893) after 10000ms
       at libcore.io.IoBridge.connectErrno(IoBridge.java:191)
       at libcore.io.IoBridge.connect(IoBridge.java:135)
       at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
       at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
       at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
       at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
       at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
       at java.net.Socket.connect(Socket.java:621)
       at okhttp3.internal.platform.Platform.connectSocket(Platform.java:120)
       at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:295)
       at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:207)
       at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:226)
       at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:106)
       at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:74)
       at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.java:255)
       at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:32)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:95)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:83)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:76)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at yudo.work.api.ServiceGenerator$SignatureInterceptor.intercept(ServiceGenerator.java:124)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at yudo.work.api.ServiceGenerator$UserAgentInterceptor.intercept(ServiceGenerator.java:84)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.java:201)
       at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.java:517)
       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:919)
  1. SSL handshake timed out
    1000 times/day with 10000 DAU android app
Non-fatal Exception: java.net.SocketTimeoutException: SSL handshake timed out
       at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_do_handshake(NativeCrypto.java)
       at com.google.android.gms.org.conscrypt.NativeSsl.doHandshake(NativeSsl.java:6)
       at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:16)
       at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:379)
       at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:337)
       at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:209)
       at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:226)
       at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:106)
       at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:74)
       at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.java:255)
       at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:32)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:95)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:83)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:76)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at yudo.work.api.ServiceGenerator$SignatureInterceptor.intercept(ServiceGenerator.java:124)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at yudo.work.api.ServiceGenerator$UserAgentInterceptor.intercept(ServiceGenerator.java:84)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:109)
       at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.java:201)
       at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.java:517)
       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:920)

@RanjithRagavan
Copy link

Do we have any update about this issue. Facing Socket timeout in Android 12 version as well.
using Okhttp latest version: 4.9.3 still the same issue.

Do we have any temporary work around.

@ljarka
Copy link

ljarka commented Jan 11, 2022

I found a workaround solution for this issue.
In my case adding ping interval helps a lot in reducing number of SocketTimeoutExceptions in the app.

OkHttpClient.Builder()
        .pingInterval(3, SECONDS)
        .build()

@arcao
Copy link

arcao commented Jan 11, 2022

I found a workaround solution for this issue. In my case adding ping interval helps a lot in reducing number of SocketTimeoutExceptions in the app.

OkHttpClient.Builder()
        .pingInterval(3, SECONDS)
        .build()

This works with Web socket and HTTP/2 only. So if you use HTTP/1, this setting is not used.

@ljarka
Copy link

ljarka commented Jan 12, 2022

I added observability to compare the difference in number of SocketTimeoutExceptions when 3 seconds ping interval is enabled and disabled. It looks promising.
@arcao is right. This solution will not work when we force HTTP/1 protocol like this:

okHttpBuilder.protocols(Collections.singletonList(Protocol.HTTP_1_1))

I didn't notice any problems with the app after adding ping interval. Users don't report any new issues.

Screenshot 2022-01-12 at 10 00 58

@yschimke
Copy link
Collaborator

Thanks for that detail results.

One thing to consider is that this ping likely keeps the radio on a lot more. So there is a battery cost, maybe can be offset but closing open connections sooner? But the results do look much better.

@mslagle-godaddy
Copy link

mslagle-godaddy commented Jan 13, 2022

Do we know what the actual issue is here? There is another thread that discusses socket timeout exceptions with respect to changing network status (offline back to online), but that doesn't seem to be the issue here. I'm also experiencing this on an array of devices. I'll try out the ping fix.

Stack trace:

java.net.SocketTimeoutException: timeout
	at okio.SocketAsyncTimeout.newTimeoutException(JvmOkio.kt:143)
	at okio.AsyncTimeout.access$newTimeoutException(AsyncTimeout.kt:162)
	at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:335)
	at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:427)
	at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:320)
	at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29)
	at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:178)
	at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:106)
	at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:79)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at a.a.a.c.a.intercept(MobileShieldInterceptor.java:17)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
	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:923)
Caused by: java.net.SocketException: Socket closed
	at java.net.SocketInputStream.read(SocketInputStream.java:209)
	at java.net.SocketInputStream.read(SocketInputStream.java:144)
	at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.readFromSocket(ConscryptEngineSocket.java:936)
	at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:900)
	at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.readUntilDataAvailable(ConscryptEngineSocket.java:815)
	at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.read(ConscryptEngineSocket.java:788)
	at okio.InputStreamSource.read(JvmOkio.kt:90)
	at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
	... 22 more

@ZakAnun
Copy link

ZakAnun commented Jan 25, 2022

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.

Hello, have you worked out this issue?

@Shlyankin
Copy link

Shlyankin commented May 11, 2022

Bug reproduced on emulator Nexus S SDK 29, okhttp3 4.9.3 (also 5.0.0-alpha.7).
We use different instances of okhtppclient for two endpoints (eg api1 and api2).
Steps:

  1. Make a successful request to api1 and api2
  2. Turn off internet connection
  3. Make an api1 request
  4. Turn on internet
  5. Catching the UnknownHostException
  6. Retry api1 request successfully
  7. Make an api2 request - timeout

Using ConnectionPool with maxIdleConnections 0 solves the problem.

@SunnySun852963
Copy link

I also face the same issue.

java.net.SocketTimeoutException: timeout
at okio.f.newTimeoutException(JvmOkio.kt:1)
at okio.AsyncTimeout.access$newTimeoutException(AsyncTimeout.kt:1)
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:6)
at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:10)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:2)
at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:1)
at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:2)
at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:1)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:25)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:12)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:4)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:12)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:27)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:12)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:22)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:12)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:7)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:12)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:16)
at okhttp3.internal.connection.RealCall.execute(RealCall.kt:5)
at com.ringcentral.pal.impl.http.XOkHttpMultiPartClient.execute(XOkHttpMultiPartClient.java:7)
at com.ringcentral.pal.impl.http.XOkHttpMultiPartClient.execute(XOkHttpMultiPartClient.java:1)
at com.ringcentral.pal.impl.network.MultiPartRequestProviderImpl.lambda$request$0(MultiPartRequestProviderImpl.java:1)
at com.ringcentral.pal.impl.network.MultiPartRequestProviderImpl.a(Unknown Source:0)
at com.ringcentral.pal.impl.network.a.run(Unknown Source:4)
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:923)
Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:144)
at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.readFromSocket(ConscryptEngineSocket.java:936)
at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:900)
at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.readUntilDataAvailable(ConscryptEngineSocket.java:815)
at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.read(ConscryptEngineSocket.java:788)
at okio.b.read(JvmOkio.kt:5)
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:3)
... 25 more

But from server we can see that the request is successful. Very strange.

@yschimke
Copy link
Collaborator

Seems likely a dupe of #3278, since comments related to turning internet off and on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Relates to usage specifically on Android
Projects
None yet
Development

No branches or pull requests