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

davsclaus 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 64392c923626 CAMEL-26911: camel-cli-connector - a send runs on a 
thread of its own, so a poll or an InOut send that waits does not hold up camel 
stop and the status (#29846)
64392c923626 is described below

commit 64392c9236269b7105e4167d7eafd43050484f1d
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 10:00:42 2026 +0100

    CAMEL-26911: camel-cli-connector - a send runs on a thread of its own, so a 
poll or an InOut send that waits does not hold up camel stop and the status 
(#29846)
    
    Part of [CAMEL-26911](https://issues.apache.org/jira/browse/CAMEL-26911), 
item B5: a slow action blocked `camel stop` and the status.
    
    ## Problem
    
    One connector thread looks for the lock file (which `camel stop` deletes), 
writes the status file, and runs the actions. A `camel cmd send` that waits (a 
`--poll` of an empty endpoint, or an InOut send to a slow route) held that 
thread up to its timeout:
    
    - `camel stop` waited for the send: 22.2 s in the test below.
    - `camel get`, `ps` and the TUI showed a status that no longer updated.
    - Other actions (route stop, dumps, ...) queued behind the send.
    
    ## Fix
    
    `LocalCliConnector.dispatchAsync` now runs every send off the connector 
thread, the same way as a repeated send already did (CAMEL-26767):
    
    - A single send runs on a new `cli-send` pool of one thread with a queue of 
100. Sends still run one at a time and in order, as they did on the connector 
thread. A full queue is answered "Busy".
    - A repeated send keeps its pool of two.
    - Both share one `send(...)` helper. The transport cancels a send when the 
client gives up the request (deletes its action file), as before.
    - `doActionSendTask` (used by a transport that dispatches on its own 
thread) uses the same `sendResult` as the async path.
    
    ## Tests
    
    `LocalCliConnectorFileTransportTest`:
    - `slowSendDoesNotBlockTheStatusNorStop`: while an InOut send to a slow 
route waits, a route-stop action is applied and shows in the status file, and 
deleting the lock file triggers sigterm. **This test fails on main.**
    - `sendsAreAnsweredInOrderOnceTheSlowOneIsDone`: a second send waits for 
the slow one, and both are answered.
    
    The whole `camel-cli-connector` module passes (113 tests).
    
    ## End-to-end
    
    A patched launcher ran an integration in the background, started `camel cmd 
send --poll --endpoint=seda:empty`, then ran `camel get` and `camel stop`:
    
    | | `camel stop` |
    |---|---|
    | main | 22.2 s |
    | this PR | 4.3 s |
    
    The send client still waits out its own timeout once the integration has 
gone, as it did before this change (`ActionClient` does not check whether the 
process is alive). That is left for a follow-up.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/cli/connector/LocalCliConnector.java     | 82 ++++++++++++++--------
 .../LocalCliConnectorFileTransportTest.java        | 45 ++++++++++++
 2 files changed, 99 insertions(+), 28 deletions(-)

diff --git 
a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
 
b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
index 0aaee7c174c9..312e850f5ae6 100644
--- 
a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
+++ 
b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
@@ -39,6 +39,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.Callable;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -131,6 +132,7 @@ public class LocalCliConnector extends ServiceSupport
     private ExecutorService semanticExecutor;
     private ExecutorService auditExecutor;
     private ExecutorService sendExecutor;
+    private ExecutorService singleSendExecutor;
     private final AtomicLong semanticConsoleLookup = new 
AtomicLong(System.nanoTime() - SEMANTIC_CONSOLE_RETRY_NANOS);
     private ProducerTemplate producer;
     private ConsumerTemplate consumer;
@@ -246,8 +248,8 @@ public class LocalCliConnector extends ServiceSupport
 
     @Override
     public CompletableFuture<Boolean> dispatchAsync(JsonObject root, 
CliActionOutput output) {
-        if ("send".equals(root.getString("action")) && 
RepeatedSend.isRepeated(root)) {
-            return repeatedSend(root, output);
+        if ("send".equals(root.getString("action"))) {
+            return RepeatedSend.isRepeated(root) ? repeatedSend(root, output) 
: singleSend(root, output);
         }
         boolean auditQuery = "semantic-audit".equals(root.getString("action"));
         if (!auditQuery && 
!"semantic-evaluate".equals(root.getString("action"))) {
@@ -302,11 +304,38 @@ public class LocalCliConnector extends ServiceSupport
      * status and the other actions go on meanwhile. The transport cancels it 
when the client gives up the request.
      */
     private CompletableFuture<Boolean> repeatedSend(JsonObject root, 
CliActionOutput output) {
+        return send(output, () -> repeatedSendResult(root), () -> {
+            if (sendExecutor == null) {
+                sendExecutor = new 
ThreadPoolBuilder(camelContext).poolSize(2).maxPoolSize(2).maxQueueSize(0)
+                        
.rejectedPolicy(ThreadPoolRejectedPolicy.Abort).build("cli-repeated-send", 
"CliRepeatedSend");
+            }
+            return sendExecutor;
+        }, "Busy: two repeated sends are already running");
+    }
+
+    /**
+     * The send action of one message, on a thread of its own, as an InOut 
send or a poll waits for the reply or the
+     * message (up to the timeout of the client), and the connector thread 
must go on writing the status and looking for
+     * the lock file, which camel stop deletes (CAMEL-26911). One at a time 
and in order, as they were on the connector
+     * thread.
+     */
+    private CompletableFuture<Boolean> singleSend(JsonObject root, 
CliActionOutput output) {
+        return send(output, () -> sendResult(root), () -> {
+            if (singleSendExecutor == null) {
+                singleSendExecutor = new 
ThreadPoolBuilder(camelContext).poolSize(1).maxPoolSize(1).maxQueueSize(100)
+                        
.rejectedPolicy(ThreadPoolRejectedPolicy.Abort).build("cli-single-send", 
"CliSend");
+            }
+            return singleSendExecutor;
+        }, "Busy: 100 sends are already waiting");
+    }
+
+    private CompletableFuture<Boolean> send(
+            CliActionOutput output, Callable<JsonObject> send, 
Callable<ExecutorService> executor, String busy) {
         CompletableFuture<Boolean> result = new CompletableFuture<>();
         FutureTask<Void> task = new FutureTask<>(() -> {
             try {
                 // capture this request's output; never use the dispatcher's 
mutable actionOutput from a worker
-                output.write(repeatedSendResult(root));
+                output.write(send.call());
                 result.complete(true);
             } catch (InterruptedException e) {
                 Thread.currentThread().interrupt();
@@ -326,14 +355,10 @@ public class LocalCliConnector extends ServiceSupport
                 if (isStoppingOrStopped()) {
                     throw new IllegalStateException("CLI connector is stopping 
or stopped");
                 }
-                if (sendExecutor == null) {
-                    sendExecutor = new 
ThreadPoolBuilder(camelContext).poolSize(2).maxPoolSize(2).maxQueueSize(0)
-                            
.rejectedPolicy(ThreadPoolRejectedPolicy.Abort).build("cli-send", 
"CliRepeatedSend");
-                }
-                sendExecutor.execute(task);
+                executor.call().execute(task);
             }
-        } catch (RejectedExecutionException busy) {
-            result.completeExceptionally(new IllegalStateException("Busy: two 
repeated sends are already running"));
+        } catch (RejectedExecutionException e) {
+            result.completeExceptionally(new IllegalStateException(busy));
         } catch (Exception e) {
             result.completeExceptionally(e);
         }
@@ -728,28 +753,25 @@ public class LocalCliConnector extends ServiceSupport
     }
 
     private void doActionSendTask(JsonObject root) throws Exception {
+        // a transport that dispatches on its own thread, rather than with 
dispatchAsync
+        writeOutput(RepeatedSend.isRepeated(root) ? repeatedSendResult(root) : 
sendResult(root));
+    }
+
+    private JsonObject sendResult(JsonObject root) throws Exception {
         DevConsole dc = 
camelContext.getCamelContextExtension().getContextPlugin(DevConsoleRegistry.class)
                 .resolveById("send");
-        if (dc != null) {
-
-            if (RepeatedSend.isRepeated(root)) {
-                // a transport that dispatches on its own thread, rather than 
with dispatchAsync
-                writeOutput(repeatedSendResult(root));
-                return;
-            }
-            waitUntilStarted();
+        if (dc == null) {
+            return new JsonObject();
+        }
+        waitUntilStarted();
 
-            final Map<String, Object> args = sendOptions(root);
-            String body = root.getString("body");
-            if (body != null) {
-                args.put("body", body);
-            }
-            args.putAll(sendHeaders(root));
-            JsonObject json = (JsonObject) dc.call(DevConsole.MediaType.JSON, 
args);
-            writeOutput(json);
-        } else {
-            writeOutput(new JsonObject());
+        final Map<String, Object> args = sendOptions(root);
+        String body = root.getString("body");
+        if (body != null) {
+            args.put("body", body);
         }
+        args.putAll(sendHeaders(root));
+        return (JsonObject) dc.call(DevConsole.MediaType.JSON, args);
     }
 
     private void waitUntilStarted() throws InterruptedException {
@@ -2147,6 +2169,10 @@ public class LocalCliConnector extends ServiceSupport
                 
camelContext.getExecutorServiceManager().shutdownNow(sendExecutor);
                 sendExecutor = null;
             }
+            if (singleSendExecutor != null) {
+                
camelContext.getExecutorServiceManager().shutdownNow(singleSendExecutor);
+                singleSendExecutor = null;
+            }
         }
         ServiceHelper.stopService(producer, consumer);
     }
diff --git 
a/dsl/camel-cli-connector/src/test/java/org/apache/camel/cli/connector/LocalCliConnectorFileTransportTest.java
 
b/dsl/camel-cli-connector/src/test/java/org/apache/camel/cli/connector/LocalCliConnectorFileTransportTest.java
index a224657e272d..262d831f4d60 100644
--- 
a/dsl/camel-cli-connector/src/test/java/org/apache/camel/cli/connector/LocalCliConnectorFileTransportTest.java
+++ 
b/dsl/camel-cli-connector/src/test/java/org/apache/camel/cli/connector/LocalCliConnectorFileTransportTest.java
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -66,6 +67,8 @@ class LocalCliConnectorFileTransportTest extends 
CamelTestSupport {
     private File dir;
     private LocalCliConnector connector;
     private final AtomicInteger sigterms = new AtomicInteger();
+    private final CountDownLatch slowEntered = new CountDownLatch(1);
+    private final CountDownLatch slowRelease = new CountDownLatch(1);
 
     @Override
     protected boolean useJmx() {
@@ -92,6 +95,12 @@ class LocalCliConnectorFileTransportTest extends 
CamelTestSupport {
             public void configure() {
                 from("direct:hello").routeId("hello").setBody(simple("Hello 
${body}"));
                 from("direct:other").routeId("other").to("mock:other");
+                from("direct:slow").routeId("slow")
+                        .process(e -> {
+                            slowEntered.countDown();
+                            slowRelease.await(20, TimeUnit.SECONDS);
+                        })
+                        .setBody(simple("Slow ${body}"));
             }
         };
     }
@@ -220,6 +229,42 @@ class LocalCliConnectorFileTransportTest extends 
CamelTestSupport {
         assertThat(out.toJson()).contains("Hello World");
     }
 
+    @Test
+    void slowSendDoesNotBlockTheStatusNorStop() throws Exception {
+        startConnector();
+        try {
+            writeAction(action("send", "endpoint", "direct:slow", "body", 
"World", "exchangePattern", "InOut"), "slow");
+            assertThat(slowEntered.await(5, TimeUnit.SECONDS)).isTrue();
+
+            // the send waits for its reply, and meanwhile the other actions 
and the status go on
+            writeAction(action("route", "command", "stop", "id", "other"), 
"fast");
+            await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertThat(
+                    routeState(readJson(file(PID + "-status.json")), 
"other")).isEqualTo("Stopped"));
+
+            // as does camel stop, which deletes the lock file
+            Files.delete(file(PID).toPath());
+            await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> 
assertThat(sigterms).hasValue(1));
+        } finally {
+            slowRelease.countDown();
+        }
+    }
+
+    @Test
+    void sendsAreAnsweredInOrderOnceTheSlowOneIsDone() throws Exception {
+        startConnector();
+        try {
+            writeAction(action("send", "endpoint", "direct:slow", "body", 
"first", "exchangePattern", "InOut"), "a");
+            assertThat(slowEntered.await(5, TimeUnit.SECONDS)).isTrue();
+            writeAction(action("send", "endpoint", "direct:hello", "body", 
"second", "exchangePattern", "InOut"), "b");
+            await().during(500, TimeUnit.MILLISECONDS).atMost(2, 
TimeUnit.SECONDS)
+                    .untilAsserted(() -> assertThat(file(PID + 
"-output-b.json")).doesNotExist());
+        } finally {
+            slowRelease.countDown();
+        }
+        assertThat(awaitJson(file(PID + 
"-output-a.json")).toJson()).contains("Slow first");
+        assertThat(awaitJson(file(PID + 
"-output-b.json")).toJson()).contains("Hello second");
+    }
+
     @Test
     void actionsWithRequestIdWriteToTheirOwnOutputFile() {
         startConnector();

Reply via email to