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

jamesnetherton 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 1c681f088be CAMEL-22207: Wait for quarkus / spring-boot process to 
complete before performing cleanup tasks
1c681f088be is described below

commit 1c681f088beadbc4ee2a3a86d7ca7c2d755754c3
Author: James Netherton <[email protected]>
AuthorDate: Fri Jul 4 08:21:30 2025 +0100

    CAMEL-22207: Wait for quarkus / spring-boot process to complete before 
performing cleanup tasks
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 43 ++++++++++++++++++++++
 1 file changed, 43 insertions(+)

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 3afb54e21e2..38bcdba303f 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
@@ -33,6 +33,7 @@ import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
 import java.nio.file.StandardOpenOption;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Supplier;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -1005,12 +1006,32 @@ public class Run extends CamelCommand {
             return 1;
         }
 
+        AtomicReference<Process> processRef = new AtomicReference<>();
+
         // create temp run dir
         Path runDirPath = Paths.get(RUN_PLATFORM_DIR, 
Long.toString(System.currentTimeMillis()));
         if (!this.background) {
             // Mark for deletion on exit
             Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                 try {
+                    // We need to wait for the process to exit before doing 
any cleanup
+                    Process process = processRef.get();
+                    if (process != null) {
+                        process.destroy();
+
+                        for (int i = 0; i < 30; i++) {
+                            if (!process.isAlive()) {
+                                break;
+                            }
+
+                            try {
+                                Thread.sleep(1000);
+                            } catch (InterruptedException e) {
+                                Thread.currentThread().interrupt();
+                            }
+                        }
+                    }
+
                     removeDir(runDirPath);
                 } catch (Exception e) {
                     // Ignore
@@ -1078,6 +1099,7 @@ public class Run extends CamelCommand {
 
         pb.inheritIO(); // run in foreground (with IO so logs are visible)
         Process p = pb.start();
+        processRef.set(p);
         this.spawnPid = p.pid();
         // wait for that process to exit as we run in foreground
         return p.waitFor();
@@ -1089,12 +1111,32 @@ public class Run extends CamelCommand {
             return 1;
         }
 
+        AtomicReference<Process> processRef = new AtomicReference<>();
+
         // create temp run dir
         Path runDirPath = Paths.get(RUN_PLATFORM_DIR, 
Long.toString(System.currentTimeMillis()));
         if (!this.background) {
             // Mark for deletion on exit
             Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                 try {
+                    // We need to wait for the process to exit before doing 
any cleanup
+                    Process process = processRef.get();
+                    if (process != null) {
+                        process.destroy();
+
+                        for (int i = 0; i < 30; i++) {
+                            if (!process.isAlive()) {
+                                break;
+                            }
+
+                            try {
+                                Thread.sleep(1000);
+                            } catch (InterruptedException e) {
+                                Thread.currentThread().interrupt();
+                            }
+                        }
+                    }
+
                     removeDir(runDirPath);
                 } catch (Exception e) {
                     // Ignore
@@ -1174,6 +1216,7 @@ public class Run extends CamelCommand {
 
         pb.inheritIO(); // run in foreground (with IO so logs are visible)
         Process p = pb.start();
+        processRef.set(p);
         this.spawnPid = p.pid();
         // wait for that process to exit as we run in foreground
         return p.waitFor();

Reply via email to