From 868e9ce285262e5277c158ea3c2082c379e81943 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 10 Oct 2017 19:03:23 +0200 Subject: [PATCH] SONAR-9721 Fix potential OkHttpClient connection leak --- .../org/sonar/ce/http/CeHttpClientImpl.java | 39 ++++++++++--------- .../server/telemetry/TelemetryClient.java | 12 +++++- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/ce/http/CeHttpClientImpl.java b/server/sonar-server/src/main/java/org/sonar/ce/http/CeHttpClientImpl.java index e119aa7948b..9f354316975 100644 --- a/server/sonar-server/src/main/java/org/sonar/ce/http/CeHttpClientImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/ce/http/CeHttpClientImpl.java @@ -51,6 +51,7 @@ public class CeHttpClientImpl implements CeHttpClient { /** * Connects to the specified JVM process and requests system information. + * * @return the system info, or absent if the process is not up or if its HTTP URL * is not registered into IPC. */ @@ -108,16 +109,17 @@ public class CeHttpClientImpl implements CeHttpClient { .post(RequestBody.create(null, new byte[0])) .url(url + "?level=" + newLogLevel.name()) .build(); - okhttp3.Response response = new OkHttpClient().newCall(request).execute(); - if (response.code() != 200) { - throw new IOException( - String.format( - "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'", - response.code(), - response.body().string(), - url)); + try (okhttp3.Response response = new OkHttpClient().newCall(request).execute()) { + if (response.code() != 200) { + throw new IOException( + String.format( + "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'", + response.code(), + response.body().string(), + url)); + } + return null; } - return null; } } @@ -145,16 +147,17 @@ public class CeHttpClientImpl implements CeHttpClient { .post(RequestBody.create(null, new byte[0])) .url(url) .build(); - okhttp3.Response response = new OkHttpClient().newCall(request).execute(); - if (response.code() != 200) { - throw new IOException( - String.format( - "Failed to trigger refresh of CE Worker count. Code was '%s' and response was '%s' for url '%s'", - response.code(), - response.body().string(), - url)); + try (okhttp3.Response response = new OkHttpClient().newCall(request).execute()) { + if (response.code() != 200) { + throw new IOException( + String.format( + "Failed to trigger refresh of CE Worker count. Code was '%s' and response was '%s' for url '%s'", + response.code(), + response.body().string(), + url)); + } + return null; } - return null; } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java index 51f5e86f461..25c0a74f9c0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java @@ -21,10 +21,12 @@ package org.sonar.server.telemetry; import java.io.IOException; +import okhttp3.Call; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; +import okhttp3.Response; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Logger; @@ -47,7 +49,7 @@ public class TelemetryClient { void upload(String json) throws IOException { Request request = buildHttpRequest(json); - okHttpClient.newCall(request).execute(); + execute(okHttpClient.newCall(request)); } void optOut(String json) { @@ -57,7 +59,7 @@ public class TelemetryClient { request.delete(body); try { - okHttpClient.newCall(request.build()).execute(); + execute(okHttpClient.newCall(request.build())); } catch (IOException e) { LOG.debug("Error when sending opt-out usage statistics: {}", e.getMessage()); } @@ -75,4 +77,10 @@ public class TelemetryClient { return config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL))); } + private static void execute(Call call) throws IOException { + try (Response ignored = call.execute()) { + // auto close connection to avoid leaked connection + } + } + } -- 2.39.5