This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 6495bb973ebf (chores) Fix SonarCloud S2095: close remaining resource 
leaks
6495bb973ebf is described below

commit 6495bb973ebfbe901274ed929ec0797a134ab687
Author: Guillaume Nodet <[email protected]>
AuthorDate: Tue Jul 7 22:54:56 2026 +0200

    (chores) Fix SonarCloud S2095: close remaining resource leaks
    
    Fix remaining SonarCloud S2095 resource leaks:
    - JfrMemoryLeakDevConsole: close Recording on startup failure, reset state
    - LRAClient: add CloseableHttpClient wrapper to properly close HttpClient
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
---
 .../org/apache/camel/service/lra/LRAClient.java    | 35 ++++++++++++++++++++--
 .../impl/console/JfrMemoryLeakDevConsole.java      | 10 ++++++-
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git 
a/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
 
b/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
index 0d9747c14075..93b3b627d955 100644
--- 
a/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
+++ 
b/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
@@ -47,7 +47,7 @@ public class LRAClient implements Closeable {
     public static final String CONTENT_TYPE = "Content-Type";
     public static final String TEXT_PLAIN_CONTENT = "text/plain";
     private final LRASagaService sagaService;
-    private final HttpClient client;
+    private final CloseableHttpClient client;
     private final String lraUrl;
 
     private static final Logger LOG = LoggerFactory.getLogger(LRAClient.class);
@@ -62,7 +62,7 @@ public class LRAClient implements Closeable {
         }
 
         this.sagaService = sagaService;
-        this.client = client;
+        this.client = new CloseableHttpClient(client);
 
         lraUrl = new LRAUrlBuilder()
                 .host(sagaService.getCoordinatorUrl())
@@ -201,5 +201,36 @@ public class LRAClient implements Closeable {
 
     @Override
     public void close() throws IOException {
+        client.close();
+    }
+
+    /**
+     * Wrapper that makes {@link HttpClient} usable in try-with-resources. On 
Java 21+ HttpClient implements
+     * AutoCloseable natively; the instanceof check future-proofs us for when 
the minimum JDK is raised.
+     */
+    private static final class CloseableHttpClient implements Closeable {
+        private final HttpClient httpClient;
+
+        CloseableHttpClient(HttpClient httpClient) {
+            this.httpClient = httpClient;
+        }
+
+        <T> CompletableFuture<HttpResponse<T>> sendAsync(
+                HttpRequest request, HttpResponse.BodyHandler<T> 
responseBodyHandler) {
+            return httpClient.sendAsync(request, responseBodyHandler);
+        }
+
+        @Override
+        public void close() throws IOException {
+            if (httpClient instanceof AutoCloseable closeable) {
+                try {
+                    closeable.close();
+                } catch (IOException e) {
+                    throw e;
+                } catch (Exception e) {
+                    throw new IOException(e);
+                }
+            }
+        }
     }
 }
diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/JfrMemoryLeakDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/JfrMemoryLeakDevConsole.java
index 0accbb3b1af3..3815b6cc1b02 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/JfrMemoryLeakDevConsole.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/JfrMemoryLeakDevConsole.java
@@ -122,8 +122,8 @@ public class JfrMemoryLeakDevConsole extends 
AbstractDevConsole {
             return errorJson("A JFR recording is already active. Stop it 
first.");
         }
 
+        Recording rec = new Recording();
         try {
-            Recording rec = new Recording();
             rec.setName("Camel OldObjectSample");
             
rec.enable("jdk.OldObjectSample").withStackTrace().withPeriod(Duration.ofSeconds(1));
             rec.enable("jdk.GarbageCollection");
@@ -165,6 +165,14 @@ public class JfrMemoryLeakDevConsole extends 
AbstractDevConsole {
             }
             return result;
         } catch (Exception e) {
+            // Clean up state in case the exception occurred after 
activeRecording was set
+            // (e.g. during auto-stop scheduling), so the console does not get 
stuck
+            // referencing a closed Recording.
+            cancelAutoStop();
+            activeRecording = null;
+            recordingStartTime = 0;
+            requestedDurationSeconds = 0;
+            rec.close();
             LOG.warn("Failed to start JFR recording: {}", e.getMessage(), e);
             return errorJson("Failed to start JFR recording: " + 
e.getMessage());
         }

Reply via email to