gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:00:57 27 4
gpt4 key购买 nike

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

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

我已经能够将该错误重现到一个极简主义的项目中 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-8221395
  • https://bugs.openjdk.java.net/browse/JDK-8236596
  • 最佳答案

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

    关于java - java.net.HttpClient 状态为 CLOSE_WAIT 的连接泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60754227/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com