Skip to content

Regression: connection timeout ignored in 1.31.0 while works in 1.27.0 #799

Closed

Description

@chanseokoh

Environment details

The same environment as in #795.

Steps to reproduce

  1. .setConnectTimeout(4000)
  2. Call https://example.com:81. It always times out after 30 seconds (on my machine) with 1.31.0. See the example code below.
    • example.com is reserved by the DNS standard, so this will always trigger timeout.

Note that I haven't tested read timeout.

Code example

  • 1.31.0: output proves that it times out after 30 seconds.
public class Main {

  public static void main(String[] args) throws IOException {
    GenericUrl url = new GenericUrl("https://example.com:81");
    HttpRequest req = new ApacheHttpTransport().createRequestFactory().buildGetRequest(url).setConnectTimeout(4000);
    long started = System.nanoTime();
    try {
      req.execute();
    } finally {
      System.out.println("elapsed (s): " + (System.nanoTime() - started) / 1000000000L);
    }
  }
}
elapsed (s): 30
Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connect to example.com:81 [example.com/93.184.216.34, example.com/2606:2800:220:1:248:1893:25c8:1946] failed: Connection timed out (Connection timed out)
	at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
  • 1.27.0: connect timeout is honored.
elapsed (s): 4
Exception in thread "main" java.net.SocketTimeoutException: connect timed out
	at java.net.PlainSocketImpl.socketConnect(Native Method)

Any additional information below

I noticed another behavioral change from 1.27.0 to 1.31.0 that 1.31.0 throws HttpHostConnectException while 1.27.0 throws java.net.SocketTimeoutException, but I think it is a bug in the Apache library in DefaultHttpClientConnectionOperator. I will file a bug against them.

            } catch (final ConnectException ex) {
                if (last) {
                    final String msg = ex.getMessage();
                    throw "Connection timed out".equals(msg)
                                    ? new ConnectTimeoutException(ex, host, addresses)
                                    : new HttpHostConnectException(ex, host, addresses);
                }

Activity

  1. chanseokoh commented on Aug 22, 2019

    @chanseokoh
    ContributorAuthor

    I have tested using Apache HttpClient directly (4.5.9 and 4.5.6) and the connect timeout works great. That makes me believe Google HTTP Client is at fault here.

      public static void main(String[] args) throws IOException {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
          HttpGet httpGet = new HttpGet("https://example.com:81");
          httpGet.setConfig(RequestConfig.custom().setConnectTimeout(1000).build());
          try (CloseableHttpResponse res = client.execute(httpGet)) {}
        }
      }

    I noticed another behavioral change from 1.27.0 to 1.31.0 that 1.31.0 throws HttpHostConnectException while 1.27.0 throws java.net.SocketTimeoutException, but I think it is a bug in the Apache library in DefaultHttpClientConnectionOperator. I will file a bug against them.

    Filed HTTPCLIENT-2013.

  2. chanseokoh commented on Aug 22, 2019

    @chanseokoh
    ContributorAuthor

    Also note that the Google HTTP Client claims that the default timeout is 20 seconds. So, the 30 second timeout is coming from somewhere else.

  3. chanseokoh commented on Aug 22, 2019

    @chanseokoh
    ContributorAuthor
      @Override
      public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
        requestConfig.setConnectionRequestTimeout(connectTimeout)
            .setSocketTimeout(readTimeout);
      }

    https://stackoverflow.com/a/37996423/1701388

    If I understand the SO link correctly, setConnectionRequestTimeout() is the deadline to wait until a connection manager returns a usable connection. For example, if all the established connections in a pool are sold out and busy, this will make the caller wait for at most the given timeout for the pool manager to return a connection. That said, I think we should also do .setConnectTimeout()?

  4. added
    triage me
    🚨
    on Aug 23, 2019
  5. added
    priority: p2
    type: bug
    on Aug 28, 2019
  6. self-assigned this
    on Aug 28, 2019
  7. removed
    triage me
    🚨
    on Aug 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority: p2type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions