java.net.SocketTimeoutException: Read timed out under Tomcat
Asked Answered
G

8

91

I have a Tomcat based web application. I am intermittently getting the following exception,

Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:532)
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:501)
    at org.apache.coyote.http11.InternalInputBuffer$InputStreamInputBuffer.doRead(InternalInputBuffer.java:563)
    at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:124)
    at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:346)
    at org.apache.coyote.Request.doRead(Request.java:422)
    at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:290)
    at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:431)
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:315)
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:200)
    at java.nio.channels.Channels$ReadableByteChannelImpl.read(Channels.java:385)

Unfortunately I don't have access to the client, so I am just trying to confirm on various reasons this can happen,

  1. Server is trying to read data from the request, but its taking longer than the timeout value for the data to arrive from the client. Timeout here would typically be Tomcat connector → connectionTimeout attribute.

  2. Client has a read timeout set, and server is taking longer than that to respond.

  3. One of the threads I went through, said this can happen with high concurrency and if the keepalive is enabled.

For #1, the initial value I had set was 20 sec, I have bumped this up to 60sec, will test, and see if there are any changes.

Meanwhile, if any of you guys can provide you expert opinion on this, that'l be really helpful. Or for that matter any other reason you can think of which might cause this issue.

Glenda answered 13/6, 2013 at 4:30 Comment(1)
I don't believe it is point 2; however, to confirm you should also set up an AccessLogValve and try and correlate this exception with a specific request.Tripody
S
59

Server is trying to read data from the request, but its taking longer than the timeout value for the data to arrive from the client. Timeout here would typically be tomcat connector -> connectionTimeout attribute.

Correct.

Client has a read timeout set, and server is taking longer than that to respond.

No. That would cause a timeout at the client.

One of the threads i went through, said this can happen with high concurrency and if the keepalive is enabled.

That is obviously guesswork, and completely incorrect. It happens if and only if no data arrives within the timeout. Period. Load and keepalive and concurrency have nothing to do with it whatsoever.

It just means the client isn't sending. You don't need to worry about it. Browser clients come and go in all sorts of strange ways.

Sika answered 13/6, 2013 at 5:45 Comment(7)
For #1 as i said, i have bumped up the connection timeout value of the tomcat connector from 20sec to 60sec. If the timeouts reduce then i'll try changing it to -1 (infinite) for testing and eventually set a more appropriate value. For #2, if suppose the client times out what error/exception should i be expecting on the server?Glenda
If the client times out reading the response, the server either won't see anything at all or else may see a 'connection reset'. I wouldn't put the server read timeout too high: it ties up a thread. If a client opens a connection to the server and doesn't send anything immediately it is misbehaving pretty badly.Sika
I do get a "connection reset" once in a while, which i attributed to client timing out (client does have a timeout set, need to figure out what is that). However i have been getting far too many "read timeouts". For which i have bumped up the connection timeout to 60sec. will monitory today if "read timeouts" reduce. My max threads is about 190 and accept count is set to 400, which i plan to reduce to 200. Since no point in having a larger queue, it will just increase the waiting time and hence the latency. Any other changes that you think i can try and might help?Glenda
Vicky, the objective isn't to get a clean log file. The objective is to service the clients that aren't misbehaving. The longer you make the read timeout, the longer that thread is tied up waiting for the misbehaving client, so the fewer threads are available to service behaving clients.Sika
Excellent explanation of the general problem, thank you! Does anyone know what file for the tomcat connector property to change that timeout?Liturgist
@tuxworld.net server.xml.Sika
The "connection timeout" does not affect anything other than making the initial connection e.g. the time it takes once a client connection is accepted, until the first put of request data is received by the server.Denominate
M
10

Here are the basic instructions:-

  1. Locate the "server.xml" file in the "conf" folder beneath Tomcat's base directory (i.e. %CATALINA_HOME%/conf/server.xml).
  2. Open the file in an editor and search for <Connector.
  3. Locate the relevant connector that is timing out - this will typically be the HTTP connector, i.e. the one with protocol="HTTP/1.1".
  4. If a connectionTimeout value is set on the connector, it may need to be increased - e.g. from 20000 milliseconds (= 20 seconds) to 120000 milliseconds (= 2 minutes). If no connectionTimeout property value is set on the connector, the default is 60 seconds - if this is insufficient, the property may need to be added.
  5. Restart Tomcat
Mila answered 19/9, 2018 at 13:10 Comment(0)
C
2
Connection.Response resp = Jsoup.connect(url) //
                .timeout(20000) //
                .method(Connection.Method.GET) //
                .execute();

actually, the error occurs when you have slow internet so try to maximize the timeout time and then your code will definitely work as it works for me.

Cathodoluminescence answered 5/3, 2019 at 15:9 Comment(1)
This is client code for a server-side problem, and actually it occurs when the client is slow sending the complete request.Sika
M
0

I had the same problem while trying to read the data from the request body. In my case which occurs randomly only to the mobile-based client devices. So I have increased the connectionUploadTimeout to 1min as suggested by this link

Melanimelania answered 4/12, 2020 at 17:23 Comment(0)
D
0

I have the same issue. The java.net.SocketTimeoutException: Read timed out error happens on Tomcat under Mac 11.1, but it works perfectly in Mac 10.13. Same Tomcat folder, same WAR file. Have tried setting timeout values higher, but nothing I do works. If I run the same SpringBoot code in a regular Java application (outside Tomcat 9.0.41 (tried other versions too), then it works also.

Mac 11.1 appears to be interfering with Tomcat.

As another test, if I copy the WAR file to an AWS EC2 instance, it works fine there too.

Spent several days trying to figure this out, but cannot resolve.

Suggestions very welcome! :)

Dispatcher answered 31/1, 2021 at 13:38 Comment(1)
I am also getting this issue fo several days but cannot fix itPushy
N
0

This happenned to my application, actually I was using a single Object which was being called by multiple functions and those were not thread safe.

Something like this :

Class A{
    Object B;
    function1(){
        B.doSomething();
    }
    function2(){
        B.doSomething();
    }
}

As they were not threadsafe, I was getting these errors :

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Socket is closed

and

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out

This is how I fixed it :

Class A{
    function1(){
        Object B;
        B.doSomething();
    }
    function2(){    
        Object B;
        B.doSomething();
    }
}

Hope it helps

Nore answered 22/6, 2022 at 10:27 Comment(0)
S
-4

It means time out from your server response. It causes due to server config and internet response.

Schechinger answered 20/3, 2021 at 15:32 Comment(0)
S
-13

I am using 11.2 and received timeouts.

I resolved by using the version of jsoup below.

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.7.2</version>
        <scope>compile</scope>
    </dependency>
Skinny answered 7/2, 2018 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.