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

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

commit 25a92568ca51f8483c30652146ac0f453f6845c5
Author: Luigi De Masi <[email protected]>
AuthorDate: Mon Nov 30 15:41:57 2026 +0100

    Fix #110: Stop timed-out wrappers before their children
    
    Capture descendants and terminate the wrapper first so it cannot continue 
to a second command as its first child exits. Exercise that case alongside 
child cleanup on the hosted CLI matrix.
    
    Co-authored-by: OpenAI Codex <[email protected]>
---
 .../java/org/apache/camel/kit/util/ProcessRunner.java   | 17 ++++++++---------
 .../org/apache/camel/kit/util/ProcessRunnerTest.java    |  4 +++-
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git 
a/kit/camel-kit-core/src/main/java/org/apache/camel/kit/util/ProcessRunner.java 
b/kit/camel-kit-core/src/main/java/org/apache/camel/kit/util/ProcessRunner.java
index b90a2308..18296afc 100644
--- 
a/kit/camel-kit-core/src/main/java/org/apache/camel/kit/util/ProcessRunner.java
+++ 
b/kit/camel-kit-core/src/main/java/org/apache/camel/kit/util/ProcessRunner.java
@@ -13,6 +13,7 @@ import java.util.Objects;
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
 
 /**
  * Runs short-lived external commands with timeout-bound output capture.
@@ -77,15 +78,13 @@ public final class ProcessRunner {
     }
 
     private static void stopProbe(Process process) {
-        try {
-            // A command shim can leave its child holding the working 
directory after the shim exits.
-            var children = process.descendants().toList().reversed();
-            children.forEach(ProcessHandle::destroyForcibly);
-            var exits = 
children.stream().map(ProcessHandle::onExit).toArray(CompletableFuture[]::new);
-            CompletableFuture.allOf(exits).completeOnTimeout(null, 1, 
TimeUnit.SECONDS).join();
-        } finally {
-            process.destroyForcibly().onExit().completeOnTimeout(null, 1, 
TimeUnit.SECONDS).join();
-        }
+        // Stop the shim first so it cannot advance to its next command when a 
child exits.
+        var children = process.descendants().toList();
+        process.destroyForcibly();
+        children.forEach(ProcessHandle::destroyForcibly);
+        var exits = Stream.concat(Stream.of(process.toHandle()), 
children.stream())
+                
.map(ProcessHandle::onExit).toArray(CompletableFuture<?>[]::new);
+        CompletableFuture.allOf(exits).completeOnTimeout(null, 1, 
TimeUnit.SECONDS).join();
     }
 
     public static Optional<Path> findOnPath(String searchPath, String command) 
{
diff --git 
a/kit/camel-kit-core/src/test/java/org/apache/camel/kit/util/ProcessRunnerTest.java
 
b/kit/camel-kit-core/src/test/java/org/apache/camel/kit/util/ProcessRunnerTest.java
index 34452c34..f7694a19 100644
--- 
a/kit/camel-kit-core/src/test/java/org/apache/camel/kit/util/ProcessRunnerTest.java
+++ 
b/kit/camel-kit-core/src/test/java/org/apache/camel/kit/util/ProcessRunnerTest.java
@@ -28,12 +28,13 @@ class ProcessRunnerTest {
     @Tag("cli-compat")
     void timedOutProbeReleasesItsChildAndWorkingDirectory(@TempDir Path root) 
throws Exception {
         Path pid = root.resolve("child.pid");
-        var result = ProcessRunner.run(root, Duration.ofSeconds(3), 
javaExecutable().toString(), "-cp",
+        var result = ProcessRunner.run(root, Duration.ofSeconds(5), 
javaExecutable().toString(), "-cp",
                 System.getProperty("java.class.path"), getClass().getName(), 
pid.toString());
         assertFalse(result.completed());
         var child = ProcessHandle.of(Long.parseLong(Files.readString(pid)));
         try {
             assertFalse(child.map(ProcessHandle::isAlive).orElse(false), 
"timed-out probe left its child running");
+            assertFalse(Files.exists(root.resolve("continued")), "timed-out 
wrapper continued after its child exited");
         } finally {
             child.ifPresent(ProcessHandle::destroyForcibly);
         }
@@ -49,6 +50,7 @@ class ProcessRunnerTest {
                 ProcessRunnerTest.class.getName()).inheritIO().start();
         Files.writeString(Path.of(args[0]), Long.toString(child.pid()));
         child.waitFor();
+        Files.createFile(Path.of(args[0]).resolveSibling("continued"));
     }
 
     private static Path javaExecutable() {

Reply via email to