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 259358b7d32d CAMEL-26912: camel-jbang - a run removes the run 
directories of runs that are gone, and camel debug keeps those of running runs 
(#29823)
259358b7d32d is described below

commit 259358b7d32d7cbab1f8a2a57c1bdf8ca8dd5431
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 13 22:02:27 2026 +0100

    CAMEL-26912: camel-jbang - a run removes the run directories of runs that 
are gone, and camel debug keeps those of running runs (#29823)
    
    ## Summary
    
    Item 8 of [CAMEL-26912](https://issues.apache.org/jira/browse/CAMEL-26912): 
**the directories of runs were left behind, or removed under other runs.**
    
    `camel run --runtime=camel-main|spring-boot|quarkus` exports the project to 
`.camel-jbang-run/<millis>`, builds it there and runs it. The directory (with 
the project and its `target/`, the application JAR) is only removed by the 
shutdown hook of the Camel CLI:
    
    - a run that is killed (`kill -9`, the terminal closed, a crash) leaves it 
behind for good, one more for each such run
    - `camel debug` (Spring Boot, Quarkus) builds its own 
`camel-jbang-debug-pom.xml` and has no run directory, but removed the whole 
`.camel-jbang-run` when it ended: the directories of the other runs in the 
directory, which were still running
    
    ### Fix
    
    - A run directory has a hidden `.camel-run-pid` file with the pid of the 
Camel CLI that runs it (hidden, as the project is exported to that directory).
    - `ProjectRunner.createRunDir()` creates it, for the three runners, and 
first removes the run directories whose process is gone (`removeStaleRunDirs`).
    - `camel debug` removes only those, not the directories of runs that are 
running.
    - A run directory without the file (of an earlier Camel version) is kept: 
it may still be running. A pid reused by another process keeps a stale 
directory until that process ends, which is harmless.
    
    With the unpacked launcher and the new camel-jbang-run jar, in a directory 
with `.camel-jbang-run/1000` (the pid of an ended process, with a `target/`) 
and `.camel-jbang-run/999` (no pid): `camel run a.camel.yaml 
--runtime=camel-main --max-seconds=8` removes `1000` when it starts, runs in a 
new directory with its pid, keeps `999`, and removes its own directory when it 
ends.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/dsl/jbang/core/commands/Debug.java       |  7 ++-
 .../golden/expected/maven-main-chaos.maven.txt     |  1 +
 .../golden/expected/maven-main-mixed.maven.txt     |  1 +
 .../resources/golden/expected/maven-main.maven.txt |  1 +
 .../golden/expected/maven-quarkus-dev.maven.txt    |  1 +
 .../golden/expected/maven-quarkus.maven.txt        |  1 +
 .../golden/expected/maven-spring-boot.maven.txt    |  1 +
 .../camel/dsl/jbang/run/CamelMainRunner.java       |  2 +-
 .../apache/camel/dsl/jbang/run/ProjectRunner.java  | 55 ++++++++++++++++++++++
 .../apache/camel/dsl/jbang/run/QuarkusRunner.java  |  2 +-
 .../camel/dsl/jbang/run/SpringBootRunner.java      |  2 +-
 .../camel/dsl/jbang/run/ProjectRunnerTest.java     | 28 +++++++++++
 12 files changed, 97 insertions(+), 5 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
index 658235b62d7a..177adcab38f7 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
@@ -49,6 +49,7 @@ import org.apache.camel.dsl.jbang.core.common.CliExitCode;
 import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
 import org.apache.camel.dsl.jbang.core.common.EnvironmentHelper;
 import org.apache.camel.dsl.jbang.core.common.ProcessFinder;
+import org.apache.camel.dsl.jbang.run.ProjectRunner;
 import org.apache.camel.dsl.jbang.run.common.ChildProcess;
 import org.apache.camel.dsl.jbang.run.common.MavenCommand;
 import org.apache.camel.dsl.jbang.run.common.PathUtils;
@@ -436,7 +437,8 @@ public class Debug extends Run {
                 AtomicReference<Process> processRef = new AtomicReference<>();
                 // the process (and those it started) stops with this JVM, 
then the files of the run are removed
                 ChildProcess.stopOnExit(processRef::get, () -> {
-                    removeDir(Paths.get(RUN_PLATFORM_DIR));
+                    // only the directories of runs that are gone, not those 
of other runs in this directory
+                    ProjectRunner.removeStaleRunDirs();
                     removeDir(CommandLineHelper.getWorkDir());
                     
Files.deleteIfExists(Paths.get("camel-jbang-debug-pom.xml"));
                 });
@@ -543,7 +545,8 @@ public class Debug extends Run {
                 AtomicReference<Process> processRef = new AtomicReference<>();
                 // the process (and those it started) stops with this JVM, 
then the files of the run are removed
                 ChildProcess.stopOnExit(processRef::get, () -> {
-                    removeDir(Paths.get(RUN_PLATFORM_DIR));
+                    // only the directories of runs that are gone, not those 
of other runs in this directory
+                    ProjectRunner.removeStaleRunDirs();
                     removeDir(CommandLineHelper.getWorkDir());
                     
Files.deleteIfExists(Paths.get("camel-jbang-debug-pom.xml"));
                 });
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-chaos.maven.txt
 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-chaos.maven.txt
index 115f78e1496e..f8bdb10b5935 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-chaos.maven.txt
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-chaos.maven.txt
@@ -16,6 +16,7 @@ ${run-dir}/target/runner.jar
 
 # project files
 .camel-export
+.camel-run-pid
 .mvn/wrapper/maven-wrapper.jar
 .mvn/wrapper/maven-wrapper.properties
 AGENTS.md
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-mixed.maven.txt
 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-mixed.maven.txt
index 38f49f5c2635..d3051a99b51f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-mixed.maven.txt
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main-mixed.maven.txt
@@ -22,6 +22,7 @@ ${run-dir}/target/runner.jar
 
 # project files
 .camel-export
+.camel-run-pid
 .mvn/wrapper/maven-wrapper.jar
 .mvn/wrapper/maven-wrapper.properties
 AGENTS.md
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main.maven.txt
 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main.maven.txt
index 6930b833224c..b960f34db8ee 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main.maven.txt
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-main.maven.txt
@@ -15,6 +15,7 @@ ${run-dir}/target/runner.jar
 
 # project files
 .camel-export
+.camel-run-pid
 .mvn/wrapper/maven-wrapper.jar
 .mvn/wrapper/maven-wrapper.properties
 AGENTS.md
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus-dev.maven.txt
 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus-dev.maven.txt
index c28773f95499..4588c80aefec 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus-dev.maven.txt
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus-dev.maven.txt
@@ -10,6 +10,7 @@ quarkus:dev
 
 # project files
 .camel-export
+.camel-run-pid
 .mvn/wrapper/maven-wrapper.jar
 .mvn/wrapper/maven-wrapper.properties
 AGENTS.md
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus.maven.txt
 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus.maven.txt
index bb11589a1a0a..4fe9bc53ba8e 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus.maven.txt
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-quarkus.maven.txt
@@ -9,6 +9,7 @@ quarkus:run
 
 # project files
 .camel-export
+.camel-run-pid
 .mvn/wrapper/maven-wrapper.jar
 .mvn/wrapper/maven-wrapper.properties
 AGENTS.md
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-spring-boot.maven.txt
 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-spring-boot.maven.txt
index f948b093ad2c..eeedf3e9dbad 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-spring-boot.maven.txt
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/resources/golden/expected/maven-spring-boot.maven.txt
@@ -9,6 +9,7 @@ spring-boot:run
 
 # project files
 .camel-export
+.camel-run-pid
 .mvn/wrapper/maven-wrapper.jar
 .mvn/wrapper/maven-wrapper.properties
 AGENTS.md
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/CamelMainRunner.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/CamelMainRunner.java
index 58aff90a105b..c339ea8d9262 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/CamelMainRunner.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/CamelMainRunner.java
@@ -102,7 +102,7 @@ final class CamelMainRunner {
         AtomicReference<Process> appRef = new AtomicReference<>();
 
         // create temp run dir
-        Path runDirPath = Paths.get(ProjectRunner.RUN_PLATFORM_DIR, 
Long.toString(System.currentTimeMillis()));
+        Path runDirPath = ProjectRunner.createRunDir();
         // the process (and those it started) stops with this JVM, then the 
files of the run are removed
         ChildProcess.Cleanup cleanup = once(() -> {
             RunPipeline.removeDir(runDirPath);
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/ProjectRunner.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/ProjectRunner.java
index 9f61c38ce6ca..264db8b08d90 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/ProjectRunner.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/ProjectRunner.java
@@ -16,7 +16,11 @@
  */
 package org.apache.camel.dsl.jbang.run;
 
+import java.io.IOException;
+import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.stream.Stream;
 
 import org.apache.camel.dsl.jbang.run.common.RuntimeType;
 import org.apache.camel.dsl.jbang.run.export.ProjectExporter;
@@ -47,6 +51,57 @@ public class ProjectRunner extends ProjectExporter {
      */
     public static final String RUN_PLATFORM_DIR = ".camel-jbang-run";
 
+    /**
+     * The file in the directory of a run with the pid of the process that 
runs it (the Camel CLI): a run that is killed
+     * or crashes cannot remove its directory, and a later run removes it 
(CAMEL-26912). The file is hidden, as the
+     * project of the run is exported to the directory.
+     */
+    static final String RUN_PID_FILE = ".camel-run-pid";
+
+    /**
+     * Creates the directory of a run in {@link #RUN_PLATFORM_DIR}, with the 
pid of this process, and removes the
+     * directories of the earlier runs whose process is gone.
+     */
+    public static Path createRunDir() throws IOException {
+        removeStaleRunDirs();
+        Path dir = Paths.get(RUN_PLATFORM_DIR, 
Long.toString(System.currentTimeMillis()));
+        Files.createDirectories(dir);
+        Files.writeString(dir.resolve(RUN_PID_FILE), 
Long.toString(ProcessHandle.current().pid()));
+        return dir;
+    }
+
+    /**
+     * Removes the directories of the runs in {@link #RUN_PLATFORM_DIR} whose 
process is gone, killed or crashed before
+     * it could remove the directory. The directories of the runs that are 
running are kept, and so are the directories
+     * without the pid of their process (of an earlier Camel version), which 
may still be running.
+     */
+    public static void removeStaleRunDirs() {
+        removeStaleRunDirs(Paths.get(RUN_PLATFORM_DIR));
+    }
+
+    static void removeStaleRunDirs(Path runDirs) {
+        if (!Files.isDirectory(runDirs)) {
+            return;
+        }
+        try (Stream<Path> dirs = Files.list(runDirs)) {
+            for (Path dir : dirs.filter(Files::isDirectory).toList()) {
+                Path pidFile = dir.resolve(RUN_PID_FILE);
+                long pid;
+                try {
+                    pid = Long.parseLong(Files.readString(pidFile).trim());
+                } catch (IOException | NumberFormatException e) {
+                    // not known which process runs it
+                    continue;
+                }
+                if 
(!ProcessHandle.of(pid).map(ProcessHandle::isAlive).orElse(false)) {
+                    RunPipeline.removeDir(dir);
+                }
+            }
+        } catch (IOException e) {
+            // best effort: the directory was removed by a later run, or 
cannot be listed (permissions, I/O)
+        }
+    }
+
     public ProjectRunner() {
         this(new RunContext());
     }
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/QuarkusRunner.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/QuarkusRunner.java
index 4d5bdbb1317e..eda39c2ab281 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/QuarkusRunner.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/QuarkusRunner.java
@@ -68,7 +68,7 @@ final class QuarkusRunner {
         AtomicReference<String> appNameRef = new AtomicReference<>();
 
         // create temp run dir
-        Path runDirPath = Paths.get(ProjectRunner.RUN_PLATFORM_DIR, 
Long.toString(System.currentTimeMillis()));
+        Path runDirPath = ProjectRunner.createRunDir();
         // the process (and those it started) stops with this JVM, then the 
files of the run are removed
         ChildProcess.Cleanup cleanup = once(() -> {
             RunPipeline.removeDir(runDirPath);
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/SpringBootRunner.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/SpringBootRunner.java
index 3640108ba8e8..70d6643da60a 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/SpringBootRunner.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/SpringBootRunner.java
@@ -70,7 +70,7 @@ final class SpringBootRunner {
         AtomicReference<String> appNameRef = new AtomicReference<>();
 
         // create temp run dir
-        Path runDirPath = Paths.get(ProjectRunner.RUN_PLATFORM_DIR, 
Long.toString(System.currentTimeMillis()));
+        Path runDirPath = ProjectRunner.createRunDir();
         // the process (and those it started) stops with this JVM, then the 
files of the run are removed
         ChildProcess.Cleanup cleanup = once(() -> {
             RunPipeline.removeDir(runDirPath);
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/ProjectRunnerTest.java
 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/ProjectRunnerTest.java
index ebbdbd88df80..cbfa4c1cc289 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/ProjectRunnerTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/ProjectRunnerTest.java
@@ -92,6 +92,8 @@ class ProjectRunnerTest {
             Path project = Path.of(build.get(3)).getParent();
             
assertThat(project.getParent().getFileName()).hasToString(ProjectRunner.RUN_PLATFORM_DIR);
             assertThat(project.resolve("pom.xml")).exists();
+            // the run has the pid of this process, so a later run can tell 
whether it is gone
+            
assertThat(project.resolve(ProjectRunner.RUN_PID_FILE)).hasContent(Long.toString(ProcessHandle.current().pid()));
             
assertThat(Files.readString(project.resolve("pom.xml"))).contains("camel-cli-connector")
                     .doesNotContain("camel-chaos");
 
@@ -116,6 +118,32 @@ class ProjectRunnerTest {
         }
     }
 
+    @Test
+    void removesTheDirectoriesOfRunsWhoseProcessIsGone() throws Exception {
+        Path runDirs = dir.resolve(ProjectRunner.RUN_PLATFORM_DIR);
+        // a process that has ended: its run was killed and could not remove 
its directory
+        Process ended = new 
ProcessBuilder(Path.of(System.getProperty("java.home"), "bin", 
"java").toString(), "-version")
+                
.redirectErrorStream(true).redirectOutput(ProcessBuilder.Redirect.DISCARD).start();
+        ended.waitFor();
+        Path gone = runDir(runDirs, "1", Long.toString(ended.pid()));
+        Path running = runDir(runDirs, "2", 
Long.toString(ProcessHandle.current().pid()));
+        // of an earlier Camel version, which has no pid: it may still be 
running
+        Path unknown = Files.createDirectories(runDirs.resolve("3"));
+
+        ProjectRunner.removeStaleRunDirs(runDirs);
+
+        assertThat(gone).doesNotExist();
+        assertThat(running.resolve("pom.xml")).exists();
+        assertThat(unknown).exists();
+    }
+
+    private static Path runDir(Path runDirs, String name, String pid) throws 
Exception {
+        Path runDir = Files.createDirectories(runDirs.resolve(name));
+        Files.writeString(runDir.resolve(ProjectRunner.RUN_PID_FILE), pid);
+        Files.writeString(runDir.resolve("pom.xml"), "<project/>");
+        return runDir;
+    }
+
     @Test
     void runStartsAndWaitsForTheIntegration() throws Exception {
         RunRequest request = request();

Reply via email to