Francois-Xavier Bonnet created HTTPCLIENT-1328:
--------------------------------------------------

             Summary: socketTimeout parameter is ignored
                 Key: HTTPCLIENT-1328
                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1328
             Project: HttpComponents HttpClient
          Issue Type: Bug
          Components: HttpClient
    Affects Versions: 4.3 Alpha1
            Reporter: Francois-Xavier Bonnet


RequestConfig.custom().setSocketTimeout(1000) is ignored if the connection is a 
new connection but works if the connection has been kept alive from a previous 
request.

I have got a page that takes 8 s to respond. This code should fail at first 
request after 1 s:

                CloseableHttpClient httpClient = HttpClients.custom().build();

                HttpGet request = new HttpGet("http://localhost:8080/slow.jsp";);
                
request.setConfig(RequestConfig.custom().setSocketTimeout(1000).build());

                long start = System.currentTimeMillis();
                // Should fail with socket timeout!
                EntityUtils.consume(httpClient.execute(request).getEntity());
                System.out.println("First request executed in " + 
(System.currentTimeMillis() - start) + " ms");

                request = new HttpGet("http://localhost:8080/slow.jsp";);
                
request.setConfig(RequestConfig.custom().setSocketTimeout(1000).build());

                start = System.currentTimeMillis();
                // Should fail with socket timeout!
                EntityUtils.consume(httpClient.execute(request).getEntity());
                System.out.println("Second request executed in " + 
(System.currentTimeMillis() - start) + " ms");

I get this output:
First request executed in 8115 ms
Exception in thread "main" java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at 
org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:130)
        ...

The first request does not respect the timeout but the second does. This comes 
from this code in 
org.apache.http.impl.execchain.MainClientExec.execute(HttpRoute, 
HttpRequestWrapper, HttpClientContext, HttpExecutionAware):
                if (!managedConn.isOpen()) {
                    this.log.debug("Opening connection " + route);
                    try {
                        establishRoute(proxyAuthState, managedConn, route, 
request, context);
                    } catch (final TunnelRefusedException ex) {
                        if (this.log.isDebugEnabled()) {
                            this.log.debug(ex.getMessage());
                        }
                        response = ex.getResponse();
                        break;
                    }
                } else {
                    final int timeout = config.getSocketTimeout();
                    if (timeout >= 0) {
                        managedConn.setSocketTimeout(timeout);
                    }
                }

The socket timeout is set only if the connection is already open.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to