-
Notifications
You must be signed in to change notification settings - Fork 9.3k
SocketTimeoutException over mobile network, fails twice and works third time #4671
Description
Hi I am having a strange issue, our API provider recently moved to TLS and we started getting timeout errors on some devices over mobile data. WIFI works fine.
It gives the timeout error twice and works fine third time. I am sure there is some kind of fallback that is happening the third time.
I am using okhttp:3.13.1
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.addInterceptor(interceptor)
.eventListener(new PrintingEventListener())
.retryOnConnectionFailure(true)
.connectTimeout(300L, TimeUnit.MILLISECONDS).writeTimeout(5L, TimeUnit.MINUTES).readTimeout(5L, TimeUnit.MINUTES);
If I increase the connectTimeout to 10 Seconds, 20 Seconds, 1 Minute, it still fails twice and works the third time.
This is the response on my OnePlus 5, Android version 9, 4G data
I/System.out: 0.000 callStart
D/OkHttp: --> GET xxxxxx
D/OkHttp: --> END GET
I/System.out: 0.010 dnsStart
I/System.out: 1.125 dnsEnd
I/System.out: 1.132 connectStart
W/System.err: java.net.SocketTimeoutException: failed to connect to xxxx/2406:da1a:881:7f00:ab8d:5b6f:e60f:9ac6 (port 443) from /2402:3a80:93f:31cf:cb68:3e51:8e44:6269 (port 48937) after 300ms
W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:129)
W/System.err: at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
W/System.err: at java.net.Socket.connect(Socket.java:621)
W/System.err: at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:71)
W/System.err: at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
W/System.err: at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:167)
W/System.err: at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
W/System.err: at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
W/System.err: at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:264)
W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:210)
W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err: at java.lang.Thread.run(Thread.java:764)
I/System.out: 1.451 connectFailed
I/System.out: 1.456 connectStart
W/System.err: java.net.SocketTimeoutException: failed to connect to xxxx/2406:da1a:881:7f01:bd15:e30f:d8d9:9010 (port 443) from /2402:3a80:93f:31cf:cb68:3e51:8e44:6269 (port 40799) after 300ms
W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:129)
W/System.err: at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
W/System.err: at java.net.Socket.connect(Socket.java:621)
W/System.err: at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:71)
W/System.err: at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
W/System.err: at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:167)
W/System.err: at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
W/System.err: at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
W/System.err: at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:264)
W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:210)
W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err: at java.lang.Thread.run(Thread.java:764)
I/System.out: 1.773 connectFailed
I/System.out: 1.777 connectStart
I/System.out: 1.892 secureConnectStart
I/System.out: 2.087 secureConnectEnd
I/System.out: 2.093 connectEnd
I/System.out: 2.095 connectionAcquired
D/OkHttp: --> GET xxxx h2
D/OkHttp: Host: xxxx
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Accept-Encoding: gzip
D/OkHttp: User-Agent: okhttp/3.13.1
D/OkHttp: --> END GET
I/System.out: 2.096 requestHeadersStart
I/System.out: 2.098 requestHeadersEnd
I/System.out: 2.099 responseHeadersStart
I/System.out: 2.303 responseHeadersEnd
I/System.out: 2.307 responseBodyStart
D/OkHttp: <-- 200 xxxx (214ms)
D/OkHttp: date: Wed, 27 Feb 2019 09:17:39 GMT
D/OkHttp: content-type: application/json;charset=UTF-8
D/OkHttp: access-control-allow-origin: *
D/OkHttp: access-control-allow-methods: POST, GET, OPTIONS, DELETE
D/OkHttp: access-control-max-age: 3600
D/OkHttp: access-control-allow-headers: token,Accept,Authorization,WWW-Authenticate,Connection,X-Requested-With,Pragma,Cache-Control,Accept-Language,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With,app-type,sessionId,source
D/OkHttp: x-application-context: application:prod:80
D/OkHttp: {"statusCode":"OK","message":"SUCCESS","data":"yyyy"}
D/OkHttp: <-- END HTTP (454-byte body)
D/OkHttp: <-- 200 xxxx (2309ms)
D/OkHttp: date: Wed, 27 Feb 2019 09:17:39 GMT
D/OkHttp: content-type: application/json;charset=UTF-8
D/OkHttp: access-control-allow-origin: *
D/OkHttp: access-control-allow-methods: POST, GET, OPTIONS, DELETE
D/OkHttp: access-control-max-age: 3600
D/OkHttp: access-control-allow-headers: token,Accept,Authorization,WWW-Authenticate,Connection,X-Requested-With,Pragma,Cache-Control,Accept-Language,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With,app-type,sessionId,source
D/OkHttp: x-application-context: application:prod:80
D/OkHttp: {"statusCode":"OK","message":"SUCCESS","data":"yyyy"}
D/OkHttp: <-- END HTTP (454-byte body)
When over Wifi
I/System.out: 0.000 callStart
D/OkHttp: --> GET xxxx
D/OkHttp: --> END GET
I/System.out: 0.057 dnsStart
I/System.out: 1.302 dnsEnd
I/System.out: 1.305 connectStart
I/System.out: 1.585 secureConnectStart
I/System.out: 2.487 secureConnectEnd
I/System.out: 2.508 connectEnd
I/System.out: 2.509 connectionAcquired
D/OkHttp: --> GET xxxx h2
D/OkHttp: Host: xxx
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Accept-Encoding: gzip
D/OkHttp: User-Agent: okhttp/3.13.1
D/OkHttp: --> END GET
I/System.out: 2.516 requestHeadersStart
I/System.out: 2.529 requestHeadersEnd
I/System.out: 2.530 responseHeadersStart
I/System.out: 2.788 responseHeadersEnd
I/System.out: 2.789 responseBodyStart
D/OkHttp: <-- 200 xxxx (275ms)
D/OkHttp: date: Wed, 27 Feb 2019 09:16:37 GMT
D/OkHttp: content-type: application/json;charset=UTF-8
D/OkHttp: access-control-allow-origin: *
D/OkHttp: access-control-allow-methods: POST, GET, OPTIONS, DELETE
D/OkHttp: access-control-max-age: 3600
D/OkHttp: access-control-allow-headers: token,Accept,Authorization,WWW-Authenticate,Connection,X-Requested-With,Pragma,Cache-Control,Accept-Language,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With,app-type,sessionId,source
D/OkHttp: x-application-context: application:prod:80
D/OkHttp: {"statusCode":"OK","message":"SUCCESS","data":"yyyy"}
D/OkHttp: <-- END HTTP (454-byte body)
D/OkHttp: <-- 200 xxxx (2747ms)
D/OkHttp: date: Wed, 27 Feb 2019 09:16:37 GMT
D/OkHttp: content-type: application/json;charset=UTF-8
D/OkHttp: access-control-allow-origin: *
D/OkHttp: access-control-allow-methods: POST, GET, OPTIONS, DELETE
D/OkHttp: access-control-max-age: 3600
D/OkHttp: access-control-allow-headers: token,Accept,Authorization,WWW-Authenticate,Connection,X-Requested-With,Pragma,Cache-Control,Accept-Language,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With,app-type,sessionId,source
D/OkHttp: x-application-context: application:prod:80
D/OkHttp: {"statusCode":"OK","message":"SUCCESS","data":"yyyy"}
D/OkHttp: <-- END HTTP (454-byte body)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@30106f47 time:153260486
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
Same is happening over different types of phones on different android versions over different mobile networks. And strangely it is working perfectly fine on some phones. Works fine over wifi. Also working fine on iOS.
I cannot prove the API information so I have replaced the sensitive info with xxxx(API endpoint) and yyyy(response)
Same is over previous version when using with retrofit 2.1.0 so Okhttp 3.3.0
Is there any ConnectionSpec I can use for this.
I have gone through hundreds of links. Please advice.