git.lucas.co / cce-ui
GPU-accelerated UI toolkit (Vulkan)
git clone https://git.lucas.co/cce-ui.git

commitedd0d2b5103df143c2781dd98dcbf091bb9e24ad
parent1a14d40d2f
authorLucas Galante <[email protected]>
date2026-06-22 08:40
Add process::spawn_detached helper to automatically reap child processes

 src/lib.rs     |  1 +
 src/process.rs | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/src/lib.rs b/src/lib.rs
index 6642d0e..ff6c2d2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,6 +7,7 @@ pub mod engine;
 pub mod scale;
 pub mod backend;
 pub mod context;
+pub mod process;
 
 pub mod colors {
     pub use crate::color::*;
diff --git a/src/process.rs b/src/process.rs
new file mode 100644
index 0000000..f942c17
--- /dev/null
+++ b/src/process.rs
@@ -0,0 +1,32 @@
+use std::process::Command;
+
+/// Spawns a process detached and automatically reaps it when it exits.
+///
+/// If an active Tokio runtime is available, it will use a background Tokio task
+/// to wait on the process. Otherwise, it will fallback to a background OS thread.
+pub fn spawn_detached(mut cmd: Command) -> std::io::Result<()> {
+    if let Ok(handle) = tokio::runtime::Handle::try_current() {
+        let mut tokio_cmd = tokio::process::Command::from(cmd);
+        let mut child = tokio_cmd.spawn()?;
+        handle.spawn(async move {
+            let _ = child.wait().await;
+        });
+    } else {
+        let mut child = cmd.spawn()?;
+        std::thread::spawn(move || {
+            let _ = child.wait();
+        });
+    }
+    Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_spawn_detached() {
+        let cmd = Command::new("true");
+        assert!(spawn_detached(cmd).is_ok());
+    }
+}