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 f4203fb01156 CAMEL-26930: camel-jbang - camel.jbang.background=true in 
application.properties starts one JVM, not a chain of them (#29910)
f4203fb01156 is described below

commit f4203fb011566aa4312572836050750e10cdd4d7
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 14 11:38:45 2026 +0100

    CAMEL-26930: camel-jbang - camel.jbang.background=true in 
application.properties starts one JVM, not a chain of them (#29910)
    
    With camel.jbang.background=true in application.properties, every JVM that 
camel run started read the same property and started another one, in an endless 
chain. A run now marks each JVM it starts to run the integration with 
CAMEL_CLI_SPAWNED_RUN=true, and a marked JVM ignores camel.jbang.background, 
camel.jbang.backgroundWait and camel.jbang.jvmDebug from the application 
properties.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/dsl/jbang/core/commands/Debug.java       |  1 +
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 37 ++++++++++++++---
 .../camel/dsl/jbang/core/commands/RunTest.java     | 46 ++++++++++++++++++++++
 3 files changed, 78 insertions(+), 6 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 177adcab38f7..dc7b8372b83b 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
@@ -607,6 +607,7 @@ public class Debug extends Run {
 
         ProcessBuilder pb = new ProcessBuilder();
         pb.command(cmds);
+        markSpawned(pb);
 
         if (background) {
             return runBackgroundProcess(pb, "Camel Debug");
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 3c31195bc1f6..0c7adefc7142 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -235,6 +235,16 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
             argument = "true|false|port")
     int jvmDebugPort;
 
+    /**
+     * The environment variable that marks a JVM a camel run started to run 
its integration: in the background, with a
+     * debugger, by camel debug, or with another Camel version. The 
application properties are read again in that JVM,
+     * and camel.jbang.background=true or camel.jbang.jvmDebug would start yet 
another JVM, and so on without end.
+     */
+    static final String SPAWNED_RUN = "CAMEL_CLI_SPAWNED_RUN";
+
+    // whether this JVM was started by a camel run to run the integration
+    boolean spawned = "true".equals(System.getenv(SPAWNED_RUN));
+
     @Option(name = "jvm-args", description = "Additional JVM arguments (e.g. 
-Xmx256m -Xms128m)")
     String jvmArgs;
 
@@ -1131,7 +1141,7 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
      * another JVM (in the background, with a debugger, camel debug, and the 
repositories of the Camel they run). The
      * run configured the request with the others ({@link 
RunProfileProperties#applyTo(RunRequest, Properties)}).
      */
-    private void applyProfileProperties(RunRequest request, Properties answer) 
{
+    void applyProfileProperties(RunRequest request, Properties answer) {
         if (answer != null) {
             // logging level/color may be configured in the properties file
             loggingOptions.loggingLevel = answer.getProperty("loggingLevel", 
loggingOptions.loggingLevel);
@@ -1149,12 +1159,16 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
                 quarkusPlatform = new QuarkusPlatformMixin();
             }
             quarkusPlatform = QuarkusPlatformMixin.of(answer, quarkusPlatform);
-            background = "true".equals(answer.getProperty(BACKGROUND, 
background ? "true" : "false"));
-            String wait = answer.getProperty(BACKGROUND_WAIT);
-            if (wait != null) {
-                backgroundWait = BackgroundWait.parse(wait);
+            // a JVM this command started runs the integration itself: the 
properties must not make it start
+            // another JVM, in the background or with a debugger, and that one 
another, without end
+            if (!spawned) {
+                background = "true".equals(answer.getProperty(BACKGROUND, 
background ? "true" : "false"));
+                String wait = answer.getProperty(BACKGROUND_WAIT);
+                if (wait != null) {
+                    backgroundWait = BackgroundWait.parse(wait);
+                }
+                jvmDebugPort = parseJvmDebugPort(answer.getProperty(JVM_DEBUG, 
Integer.toString(jvmDebugPort)));
             }
-            jvmDebugPort = parseJvmDebugPort(answer.getProperty(JVM_DEBUG, 
Integer.toString(jvmDebugPort)));
         }
         // what this command reads itself to run in another JVM (with another 
Camel version)
         camelVersion = request.camelVersion;
@@ -1188,6 +1202,7 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
 
         ProcessBuilder pb = new ProcessBuilder();
         pb.command(jbangArgs);
+        markSpawned(pb);
 
         if (verbose) {
             printer().println(String.join(" ", jbangArgs));
@@ -1305,6 +1320,14 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
         return jbangArgs;
     }
 
+    /**
+     * Marks the JVM this command starts to run the integration (in the 
background, with a debugger, camel debug, or
+     * with another Camel version), so it does not start one again (see {@link 
#SPAWNED_RUN}).
+     */
+    static void markSpawned(ProcessBuilder pb) {
+        pb.environment().put(SPAWNED_RUN, "true");
+    }
+
     protected int runBackground(KameletMain main) throws Exception {
         List<String> cmds;
         if (originalArgs() != null) {
@@ -1331,6 +1354,7 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
     }
 
     protected int runBackgroundProcess(ProcessBuilder pb, String kind) throws 
Exception {
+        markSpawned(pb);
         // the output goes to ~/.camel/<pid>-run.log, as nobody reads the 
output of a process in the background
         BackgroundLog.Started started = BackgroundLog.start(pb);
         Process p = started.process();
@@ -1469,6 +1493,7 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
 
         ProcessBuilder pb = new ProcessBuilder();
         pb.command(jbangArgs);
+        markSpawned(pb);
         if (background) {
             return runBackgroundProcess(pb, "Camel Main");
         } else {
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunTest.java
index 784366cb57c4..6365265ee42f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.dsl.jbang.core.commands;
 
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Properties;
 
 import org.apache.camel.FailedToStartRouteException;
 import org.apache.camel.dsl.jbang.core.common.CliException;
@@ -267,6 +268,51 @@ class RunTest extends CamelCommandBaseTestSupport {
         assertThat(request.dev).isTrue();
     }
 
+    @Test
+    void theBackgroundAndDebugPropertiesStartAnotherJvm() {
+        Run command = new Run(new CamelJBangMain());
+        CommandLineTestHelper.parse(command, "route.yaml");
+        command.spawned = false;
+
+        command.applyProfileProperties(command.toRunRequest(null), 
backgroundAndDebugProperties());
+
+        assertThat(command.background).isTrue();
+        assertThat(command.backgroundWait).isEqualTo(5000);
+        assertThat(command.jvmDebugPort).isEqualTo(4004);
+    }
+
+    @Test
+    void aJvmThatARunStartedDoesNotStartAnotherFromTheProperties() {
+        // the JVM in the background reads the same application.properties: 
before, it went to the background
+        // again, and its own child too, without end (CAMEL-26930)
+        Run command = new Run(new CamelJBangMain());
+        CommandLineTestHelper.parse(command, "route.yaml");
+        command.spawned = true;
+
+        command.applyProfileProperties(command.toRunRequest(null), 
backgroundAndDebugProperties());
+
+        assertThat(command.background).isFalse();
+        assertThat(command.backgroundWait).isEqualTo(BackgroundWait.DEFAULT);
+        assertThat(command.jvmDebugPort).isZero();
+    }
+
+    @Test
+    void theJvmsARunStartsAreMarked() {
+        ProcessBuilder pb = new ProcessBuilder("camel", "run", "route.yaml");
+
+        Run.markSpawned(pb);
+
+        assertThat(pb.environment()).containsEntry(Run.SPAWNED_RUN, "true");
+    }
+
+    private static Properties backgroundAndDebugProperties() {
+        Properties properties = new Properties();
+        properties.setProperty("camel.jbang.background", "true");
+        properties.setProperty("camel.jbang.backgroundWait", "5000");
+        properties.setProperty("camel.jbang.jvmDebug", "true");
+        return properties;
+    }
+
     @Test
     public void shouldParseDataDirOption() throws Exception {
         Run command = new Run(new CamelJBangMain());

Reply via email to