Эх сурвалжийг харах

ctrlc ignore err if already set

Thomas 7 сар өмнө
parent
commit
b485ce70a9
1 өөрчлөгдсөн 20 нэмэгдсэн , 6 устгасан
  1. 20 6
      src/runners.rs

+ 20 - 6
src/runners.rs

@@ -91,7 +91,6 @@ pub struct RunReport {
     pub log: String,
 }
 
-
 impl RunReport {
     /// Serialize the RunReport to a JSON string and save it to `file_prefix<uuid>.log.gz`.
     ///
@@ -173,16 +172,31 @@ impl Run for DockerRun {
     /// * An `anyhow::Error` if setup or execution fails.
     fn run(&mut self) -> anyhow::Result<()> {
         // Sets up a Ctrl-C handler to stop Docker containers on interrupt.
-        ctrlc::try_set_handler(move || {
-            if let Ok(container_id) = DOCKER_ID.lock() {
-                for id in container_id.iter() {
+        // ctrlc::try_set_handler(move || {
+        //     if let Ok(container_id) = DOCKER_ID.lock() {
+        //         for id in container_id.iter() {
+        //             warn!("Stopping Docker container {id}...");
+        //             let _ = Command::new("docker").args(["stop", id]).status();
+        //         }
+        //     }
+        //     std::process::exit(1);
+        // })
+        // .context("Failed to set Ctrl-C handler")?;
+
+        if let Err(e) = ctrlc::try_set_handler(move || {
+            if let Ok(container_ids) = DOCKER_ID.lock() {
+                for id in container_ids.iter() {
                     warn!("Stopping Docker container {id}...");
                     let _ = Command::new("docker").args(["stop", id]).status();
                 }
             }
             std::process::exit(1);
-        })
-        .context("Failed to set Ctrl-C handler")?;
+        }) {
+            // Ignore if a handler was already set; propagate all other errors
+            if !matches!(e, ctrlc::Error::MultipleHandlers) {
+                return Err(e).context("Failed to set Ctrl-C handler")?;
+            }
+        }
 
         // Configures memory limits for the Docker container.
         let c = Config::default();