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 bfd0ce4aaaba CAMEL-27149: camel-jbang - camel stop <pid> of the 
process that launched an integration stops that integration (#30321)
bfd0ce4aaaba is described below

commit bfd0ce4aaabaa372959e0b44ead49da0a7438346
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 18 10:01:41 2026 +0100

    CAMEL-27149: camel-jbang - camel stop <pid> of the process that launched an 
integration stops that integration (#30321)
    
    
    * CAMEL-27149: shorter sentences in the camel stop docs
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../modules/ROOT/pages/camel-jbang-managing.adoc   |  4 +-
 .../jbang/core/commands/process/StopProcess.java   | 35 +++++++++++-
 .../core/commands/process/StopProcessTest.java     | 66 ++++++++++++++++++++++
 3 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang-managing.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-jbang-managing.adoc
index bdd258082a04..be133d07e4a4 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang-managing.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang-managing.adoc
@@ -36,7 +36,9 @@ stopped in time, it exits with code `124`, and `--kill` stops 
it at once. Use `-
 NOTE: The name is matched as a prefix — `camel stop d` stops all integrations 
starting with _d_ — unless an
 integration has exactly that name: with `dude` and `dude2` running, `camel 
stop dude` stops only `dude`, and
 `camel stop dude*` stops both. Append `!` to match only that name (`camel stop 
dude!`), or give the PID.
-A PID must be a running Camel integration, and a name or PID that matches 
nothing exits with code `3`
+A PID can be a running Camel integration or its launcher, such as the JBang 
launcher of `camel run`, whose pid test
+tools like Citrus keep. `camel stop` then stops the Camel integrations the 
launcher started, without signalling the
+launcher. A name or PID that matches nothing exits with code `3`
 (see xref:camel-jbang-exit-codes.adoc[Exit Codes and Output]).
 
 Restart an integration (stop + re-launch):
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
index f1954e45bd42..631d24b45476 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
@@ -21,6 +21,7 @@ import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
+import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
@@ -78,7 +79,10 @@ public class StopProcess extends ProcessBaseCommand {
         if (!all && (name == null || name.isBlank())) {
             requireOneRunning();
         }
-        List<Long> pids = findIntegrationPids(all ? "*" : name);
+        List<Long> pids = launchedBy(all ? null : name);
+        if (pids.isEmpty()) {
+            pids = findIntegrationPids(all ? "*" : name);
+        }
 
         for (Long pid : pids) {
             Path pidFile = 
CommandLineHelper.getCamelDir().resolve(Long.toString(pid));
@@ -139,6 +143,35 @@ public class StopProcess extends ProcessBaseCommand {
         }
     }
 
+    /**
+     * The running Camel integrations that the process with this pid launched, 
when the pid is not a Camel integration
+     * itself: such as the JBang launcher of camel run, whose pid Citrus 
keeps, while the Camel JVM is its child. Camel
+     * 4.23 signalled a pid as it was given; since CAMEL-26911 only Camel 
integrations are signalled, so the
+     * integrations below the launcher are stopped instead (CAMEL-27149).
+     *
+     * @param  nameOrPid the argument of camel stop
+     * @return           the pids of the integrations, or an empty list when 
the argument is not a pid, is a Camel
+     *                   integration, or launched none
+     */
+    private List<Long> launchedBy(String nameOrPid) {
+        if (nameOrPid == null || !nameOrPid.matches("\\d+") || 
!ProcessFinder.find(nameOrPid).isEmpty()) {
+            return List.of();
+        }
+        long launcher = Long.parseLong(nameOrPid);
+        Set<Long> descendants = ProcessHandle.of(launcher)
+                .map(ph -> 
ph.descendants().map(ProcessHandle::pid).collect(Collectors.toSet()))
+                .orElse(Set.of());
+        if (descendants.isEmpty()) {
+            return List.of();
+        }
+        List<Long> pids = 
ProcessFinder.findAll().stream().map(ProcessFinder.CamelProcess::pid)
+                
.filter(descendants::contains).collect(Collectors.toCollection(ArrayList::new));
+        if (!pids.isEmpty()) {
+            printer().println("PID " + launcher + " is not a Camel 
integration: stopping the Camel integration it launched");
+        }
+        return pids;
+    }
+
     /**
      * Waits until the process has exited.
      *
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcessTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcessTest.java
index 9650a5c4b67d..a7bdcacc0855 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcessTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcessTest.java
@@ -36,7 +36,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -235,6 +238,69 @@ class StopProcessTest extends ProcessCommandTestSupport {
         assertEquals(CliExitCode.USAGE, e.getExitCode());
     }
 
+    @Test
+    void testStopByThePidOfTheLauncherStopsTheIntegrationItLaunched() throws 
Exception {
+        // CAMEL-27149: Citrus keeps the pid of the JBang launcher of camel 
run, and the Camel JVM is its child
+        long launcherPid = TEST_PID;
+        long integrationPid = TEST_PID + 1;
+        Path pidFile = running(integrationPid, "orders");
+        StopProcess command = new StopProcess(new 
CamelJBangMain().withPrinter(printer));
+        command.name = Long.toString(launcherPid);
+
+        try (MockedStatic<ProcessHandle> mocked = 
mockStatic(ProcessHandle.class)) {
+            ProcessHandle integration = mockProcessHandle(integrationPid);
+            ProcessHandle launcher = mockLauncher(launcherPid, integration);
+            ProcessHandle current = mockCurrentHandle();
+            mocked.when(ProcessHandle::current).thenReturn(current);
+            mocked.when(ProcessHandle::allProcesses).thenAnswer(inv -> 
Stream.of(launcher, integration));
+            mocked.when(() -> 
ProcessHandle.of(launcherPid)).thenReturn(Optional.of(launcher));
+            mocked.when(() -> 
ProcessHandle.of(integrationPid)).thenReturn(Optional.of(integration));
+            
when(integration.onExit()).thenReturn(CompletableFuture.completedFuture(integration));
+
+            assertEquals(0, command.doCall());
+
+            assertFalse(Files.exists(pidFile), "the integration is stopped");
+            assertTrue(printer.getOutput().contains(
+                    "PID 12345 is not a Camel integration: stopping the Camel 
integration it launched"),
+                    printer.getOutput());
+            assertTrue(printer.getOutput().contains("Shutting down Camel 
integration (PID: 12346)"), printer.getOutput());
+            verify(launcher, never()).destroy();
+            verify(launcher, never()).destroyForcibly();
+        }
+    }
+
+    @Test
+    void testStopByThePidOfAProcessThatLaunchedNoIntegrationFails() throws 
Exception {
+        StopProcess command = new StopProcess(new 
CamelJBangMain().withPrinter(printer));
+        command.name = Long.toString(TEST_PID);
+
+        try (MockedStatic<ProcessHandle> mocked = 
mockStatic(ProcessHandle.class)) {
+            // a process that is not a Camel integration, with a child that is 
not one either
+            ProcessHandle child = mock(ProcessHandle.class);
+            lenient().when(child.pid()).thenReturn(TEST_PID + 1);
+            ProcessHandle other = mockLauncher(TEST_PID, child);
+            ProcessHandle current = mockCurrentHandle();
+            mocked.when(ProcessHandle::current).thenReturn(current);
+            mocked.when(ProcessHandle::allProcesses).thenAnswer(inv -> 
Stream.of(other));
+            mocked.when(() -> 
ProcessHandle.of(TEST_PID)).thenReturn(Optional.of(other));
+
+            CliException e = assertThrows(CliException.class, command::doCall);
+
+            assertEquals(CliExitCode.NOT_FOUND, e.getExitCode());
+            assertEquals("No running Camel integration matches: 12345", 
e.getMessage());
+            verify(other, never()).destroy();
+            verify(other, never()).destroyForcibly();
+        }
+    }
+
+    // a process that is not a Camel integration (no status file), with a 
child process
+    private static ProcessHandle mockLauncher(long pid, ProcessHandle child) {
+        ProcessHandle launcher = mock(ProcessHandle.class);
+        lenient().when(launcher.pid()).thenReturn(pid);
+        lenient().when(launcher.descendants()).thenAnswer(inv -> 
Stream.of(child));
+        return launcher;
+    }
+
     // an integration that runs: its status file and its pid file, which camel 
stop deletes
     private static Path running(long pid, String name) throws Exception {
         writeStatusFile(pid, buildContextStatus(name, 5));

Reply via email to