question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Distinguish between connect timeout and read timeout

See original GitHub issue

Feature request: Throw identifiable exceptions for read timeout and connect timeout. We wish to retry on connect timeout errors and not on read timeouts. The okHttp client throws the same exception in both cases. The only way to distinguish between the two is the through the error message, which is not clean or reliable.

On read timeout: java.net.SocketTimeoutException: timeout at okio.Okio$3.newTimeoutException(Okio.java:212) at okio.AsyncTimeout.exit(AsyncTimeout.java:288) at okio.AsyncTimeout$2.read(AsyncTimeout.java:242) 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:184) at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125) at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775) at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86) at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760) at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613) at okhttp3.RealCall.getResponse(RealCall.java:244) at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201) at com.bluejeans.sample.test.OkHttpConnect$1.intercept(OkHttpConnect.java:25) at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163) at okhttp3.RealCall.execute(RealCall.java:57) at com.bluejeans.sample.test.OkHttpConnect.main(OkHttpConnect.java:39) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at okio.Okio$2.read(Okio.java:140) at okio.AsyncTimeout$2.read(AsyncTimeout.java:238) ... 16 more

On connect timeout: java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at okhttp3.internal.Platform.connectSocket(Platform.java:121) at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:185) at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:170) at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111) at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187) at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123) at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93) at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296) at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) at okhttp3.RealCall.getResponse(RealCall.java:243) at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201) at com.bluejeans.sample.test.OkHttpConnect$1.intercept(OkHttpConnect.java:25) at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163) at okhttp3.RealCall.execute(RealCall.java:57) at com.bluejeans.sample.test.OkHttpConnect.main(OkHttpConnect.java:39)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
MartyIXcommented, Mar 25, 2018

I would appreciate this feature very much. I would like to help a little, so I tried to simulate both scenarios on the latest version of okhttp (3.10.0).

Connect timeout

To simulate connection timeout, I tried to connect to http://10.255.255.1 (see) and the exception I get is:

java.net.ConnectException: Failed to connect to /10.255.255.1:80

	at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:242)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)
	at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
	at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
	at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
	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:126)
	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.execute(RealCall.java:77)
	at com.project.shared.utils.http.Client.send(Client.java:85)
	at com.project.shared.utils.http.ClientTest.Send(ClientTest.groovy:13)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.base/java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
	at java.base/java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
	at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240)
	... 19 more

Read timeout

For read timeout, the exception is as follows:

java.net.SocketTimeoutException: Read timed out

	at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
	at java.base/java.net.SocketInputStream.read(SocketInputStream.java:171)
	at java.base/java.net.SocketInputStream.read(SocketInputStream.java:141)
	at okio.Okio$2.read(Okio.java:140)
	at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
	at okio.RealBufferedSource.indexOf(RealBufferedSource.java:355)
	at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:227)
	at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
	at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
	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:126)
	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.execute(RealCall.java:77)
	at com.project.shared.utils.http.Client.send(Client.java:85)
	at com.project.shared.utils.http.ClientTest.read timeout(ClientTest.groovy:27)

(I set readTimeout(10, TimeUnit.MILLISECONDS) to simulate this)

My environment:

  • okhttp 3.10
  • java 9.0.4
1reaction
swankjessecommented, Nov 4, 2018

Yeah, but how do we know whether the server received the request? The only reliable way is to receive a response back from the server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection Timeout vs. Read Timeout for Java Sockets
From the client side, the “read timed out” error happens if the server is taking longer to respond and send information. This could...
Read more >
Difference between Connection Timeout and ... - Google Groups
The connection timeout looks at traffic in both directions, while the read timeout is the application reading from the browser, thus if you ......
Read more >
Read and Connection Timeout Handling | Paylosophy
Read timeout usually occurs within 40 to 60 seconds. Read timeout occurs when the socket is open, connection to the host server is...
Read more >
Write Timeout - levups.com
This is the most useful and easy to estimate timeout of all: the time you wait for the response. It might be quick...
Read more >
What is the Difference Between the Connect Timeout, Read ...
Oracle WebLogic Server - Version 10.3.6 and later: What is the Difference Between the Connect Timeout, Read Timeout and JDBC Statement ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found