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 85dbde2841f283a1257410e6a4ed050061b9f927 Author: Luigi De Masi <[email protected]> AuthorDate: Mon Nov 30 15:23:14 2026 +0100 Fix #110: Reap children of timed-out prerequisite probes A stalled command shim can leave its child holding the project directory after Doctor returns. Stop both processes with bounded waits and exercise the child lifecycle in the CLI compatibility matrix. Co-authored-by: OpenAI Codex <[email protected]> --- .../org/apache/camel/kit/util/ProcessRunner.java | 28 +++++++++++++++---- .../apache/camel/kit/util/ProcessRunnerTest.java | 32 +++++++++++++++++++++- 2 files changed, 53 insertions(+), 7 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 3bc533f1..b90a2308 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 @@ -11,6 +11,8 @@ import java.time.Duration; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; /** * Runs short-lived external commands with timeout-bound output capture. @@ -57,18 +59,32 @@ public final class ProcessRunner { drainer.setDaemon(true); drainer.start(); - if (!process.waitFor(timeout.toMillis(), java.util.concurrent.TimeUnit.MILLISECONDS)) { - process.destroyForcibly(); + if (!process.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS)) { return new Result(false, -1, output.toString().trim()); } drainer.join(500); return new Result(true, process.exitValue(), output.toString().trim()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return new Result(false, -1, ""); } catch (Exception e) { - if (process != null) { - process.destroyForcibly(); - } return new Result(false, -1, ""); + } finally { + if (process != null && process.isAlive()) + stopProbe(process); + } + } + + 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(); } } @@ -81,7 +97,7 @@ public final class ProcessRunner { for (String entry : searchPath.split(File.pathSeparator, -1)) { try { Path directory = Path.of(entry); - // Resolve bare commands only in absolute PATH directories. + // A relative PATH entry could select an executable from the project being inspected. if (!directory.isAbsolute()) continue; for (String suffix : suffixes) { 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 8ee587f8..34452c34 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 @@ -1,10 +1,13 @@ package org.apache.camel.kit.util; +import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.util.Locale; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.*; @@ -21,7 +24,34 @@ class ProcessRunnerTest { assertTrue(result.output().contains("version")); } - private Path javaExecutable() { + @Test + @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", + 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"); + } finally { + child.ifPresent(ProcessHandle::destroyForcibly); + } + } + + public static void main(String[] args) throws Exception { + if (args.length == 0) { + Thread.sleep(30_000); + return; + } + var child = new ProcessBuilder( + javaExecutable().toString(), "-cp", System.getProperty("java.class.path"), + ProcessRunnerTest.class.getName()).inheritIO().start(); + Files.writeString(Path.of(args[0]), Long.toString(child.pid())); + child.waitFor(); + } + + private static Path javaExecutable() { String binary = System.getProperty("os.name", "").toLowerCase(Locale.ROOT).contains("win") ? "java.exe" : "java";
