状态 CLOSE_WAIT 和 java.net.HttpClient 的连接泄漏

Connections leaking with state CLOSE_WAIT with java.net.HttpClient

我们正在使用 openJDK11.0.6 java.net.http HTTP (HTTP1.1) 客户端从网站获取内容。经过很长的执行时间后,我们注意到性能下降。 CPU 被 100% 使用,即使应用程序什么也不做。我们能够确定它来自大量应用程序泄漏的套接字(CLOSE-WAIT 状态)。

这里已经有一些关于它的问题(比如 ),但是所有提交的错误都应该被修复并向后移植到 java 11.0.6。

我已经能够将错误重现到一个 minalist 项目中 on github。似乎只有当客户端从服务器收到 204 NO-CONTENT 时才会发生。

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;

public class BasicFetcherApp {

  public static void main(String[] args) throws Exception {
    System.out.println("App is running... pid: " + ProcessHandle.current().pid());
    HttpClient client = HttpClient.newBuilder().version(Version.HTTP_1_1).build();
    for (int i = 0; i < 10; ++i) {
      HttpRequest request =
          HttpRequest.newBuilder(URI.create("http://localhost:4001/nocontent")).build();
      HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
      System.out.println(String.format("Response HTTP status: %s", response.statusCode()));
    }
    System.out.println("Finish");
    System.in.read();
  }
}

如果我们运行它:

App is running... pid: 23306
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Response HTTP status: 204
Finish
ss -np | grep 23306
u_str            ESTAB                  0                   0                                                                                                 * 4121660                                                        * 0               users:(("java",pid=23306,fd=13))                                               
u_str            ESTAB                  0                   0                                                                                                 * 4121047                                                        * 0               users:(("java",pid=23306,fd=6))                                                
tcp              ESTAB                  0                   0                                                                                [::ffff:127.0.0.1]:56666                                         [::ffff:127.0.0.1]:40177           users:(("java",pid=23306,fd=7))                                                
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58592                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=24))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58570                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=18))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58572                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=19))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58564                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=15))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58560                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=14))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58574                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=20))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58582                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=21))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58590                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=23))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58594                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=25))                                               
tcp              CLOSE-WAIT             1                   0                                                                                [::ffff:127.0.0.1]:58584                                         [::ffff:127.0.0.1]:4001            users:(("java",pid=23306,fd=22))                                               

OpenJDK 错误(已修复):

这可能是 https://bugs.openjdk.java.net/browse/JDK-8216974,已在 JDK 13

中修复