Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8205007

HttpClient hang when multiple threads get 100k bytes concurrently

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 11
    • core-libs

      Run the following java program, it will hang:

      import java.io.IOException;
      import java.net.Socket;
      import java.net.URI;
      import java.net.http.HttpClient;
      import java.net.http.HttpRequest;
      import java.net.http.HttpResponse;
      import java.security.KeyManagementException;
      import java.security.NoSuchAlgorithmException;
      import java.security.cert.CertificateException;
      import java.security.cert.X509Certificate;

      import javax.net.ssl.SSLContext;
      import javax.net.ssl.SSLEngine;
      import javax.net.ssl.TrustManager;
      import javax.net.ssl.X509ExtendedTrustManager;

      public class TestHttpStress {
          private HttpClient client;

          private URI target_get = new URI("https", null, <server name>, 8443,
                  "/get", "size=" + 100000, null);

          public TestHttpStress() throws Exception {
              client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2)
                      .sslContext(getSslContext()).build();
          }

          public static void main(String[] args) throws Exception {
              final int thread_num = Integer.parseInt(args[0]);
              TestHttpStress th = new TestHttpStress();
              for (int i = 0; i < thread_num; i++) {
                  Thread t = new Thread(() -> th.run());
                  t.start();
              }

          }

          public void run() {
              long i = 0;
              while (true) {
                  int r = 0;
                  try {
                      r = get();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  System.out.println(r + " " + i++);

              }
          }

          private int get() throws IOException, InterruptedException {
              HttpRequest request = HttpRequest.newBuilder(target_get).GET().build();
              HttpResponse<byte[]> response = client.send(request,
                      HttpResponse.BodyHandlers.ofByteArray());
              return response.body().length;
          }

          private SSLContext getSslContext()
                  throws NoSuchAlgorithmException, KeyManagementException {
              TrustManager[] trustAllCerts = new TrustManager[] {
                      new X509ExtendedTrustManager() {

                          @Override
                          public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                                  throws CertificateException {
                              // TODO Auto-generated method stub

                          }

                          @Override
                          public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                                  throws CertificateException {
                              // TODO Auto-generated method stub

                          }

                          @Override
                          public X509Certificate[] getAcceptedIssuers() {
                              // TODO Auto-generated method stub
                              return null;
                          }

                          @Override
                          public void checkClientTrusted(X509Certificate[] arg0, String arg1,
                                  Socket arg2) throws CertificateException {
                              // TODO Auto-generated method stub

                          }

                          @Override
                          public void checkClientTrusted(X509Certificate[] arg0, String arg1,
                                  SSLEngine arg2) throws CertificateException {
                              // TODO Auto-generated method stub

                          }

                          @Override
                          public void checkServerTrusted(X509Certificate[] arg0, String arg1,
                                  Socket arg2) throws CertificateException {
                              // TODO Auto-generated method stub

                          }

                          @Override
                          public void checkServerTrusted(X509Certificate[] arg0, String arg1,
                                  SSLEngine arg2) throws CertificateException {
                              // TODO Auto-generated method stub

                          }

                      } };

              SSLContext sslContext = SSLContext.getInstance("SSL");
              sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
              return sslContext;
          }
      }

      This issue happened under both h2 and h2c although the example code is h2. Btw, I didn't observe it happened for 10k or less, but it happened frequently for 100k. More information is in the bug comment.

            dfuchs Daniel Fuchs
            fyuan Frank Yuan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: