[Java][HttpClient] Fix memory leak with virtual threads (#21729) (#21752)

This commit is contained in:
Adam Juraszek
2025-08-22 08:41:12 +02:00
committed by GitHub
parent bae8082b9a
commit dbe0419034
3 changed files with 18 additions and 21 deletions

View File

@@ -56,7 +56,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.function.Supplier;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -149,8 +148,8 @@ public class ApiClient extends JavaTimeFormatter {
protected Map<String, Authentication> authentications;
protected Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
protected Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();
protected ThreadLocal<Integer> lastStatusCode = new ThreadLocal<>();
protected ThreadLocal<Map<String, List<String>>> lastResponseHeaders = new ThreadLocal<>();
protected DateFormat dateFormat;
@@ -302,7 +301,7 @@ public class ApiClient extends JavaTimeFormatter {
*/
@Deprecated
public int getStatusCode() {
return lastStatusCodeByThread.get(Thread.currentThread().getId());
return lastStatusCode.get();
}
/**
@@ -311,7 +310,7 @@ public class ApiClient extends JavaTimeFormatter {
*/
@Deprecated
public Map<String, List<String>> getResponseHeaders() {
return lastResponseHeadersByThread.get(Thread.currentThread().getId());
return lastResponseHeaders.get();
}
/**
@@ -1025,13 +1024,13 @@ public class ApiClient extends JavaTimeFormatter {
protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
int statusCode = response.getCode();
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
lastStatusCode.set(statusCode);
if (statusCode == HttpStatus.SC_NO_CONTENT) {
return null;
}
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
lastResponseHeaders.set(responseHeaders);
if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType);