Browse Source

introduce RAIIChild to drop the child process for desktop even when the exit is not planned

Evan Almloff 1 year ago
parent
commit
1683ec8a72
2 changed files with 12 additions and 12 deletions
  1. 12 5
      packages/cli/src/server/desktop/mod.rs
  2. 0 7
      packages/web/src/lib.rs

+ 12 - 5
packages/cli/src/server/desktop/mod.rs

@@ -210,7 +210,7 @@ fn send_msg(msg: HotReloadMsg, channel: &mut impl std::io::Write) -> bool {
     }
 }
 
-fn start_desktop(config: &CrateConfig, skip_assets: bool) -> Result<(Child, BuildResult)> {
+fn start_desktop(config: &CrateConfig, skip_assets: bool) -> Result<(RAIIChild, BuildResult)> {
     // Run the desktop application
     let result = crate::builder::build_desktop(config, true, skip_assets)?;
 
@@ -223,9 +223,9 @@ fn start_desktop(config: &CrateConfig, skip_assets: bool) -> Result<(Child, Buil
                 file.set_extension("exe");
             }
             let active = "DIOXUS_ACTIVE";
-            let child = Command::new(file.to_str().unwrap())
+            let child = RAIIChild(Command::new(file.to_str().unwrap())
                 .env(active, "true")
-                .spawn()?;
+                .spawn()?);
 
             Ok((child, result))
         }
@@ -233,7 +233,7 @@ fn start_desktop(config: &CrateConfig, skip_assets: bool) -> Result<(Child, Buil
 }
 
 pub(crate) struct DesktopPlatform {
-    currently_running_child: Child,
+    currently_running_child: RAIIChild,
     skip_assets: bool,
 }
 
@@ -261,9 +261,16 @@ impl Platform for DesktopPlatform {
     }
 
     fn rebuild(&mut self, config: &CrateConfig) -> Result<BuildResult> {
-        self.currently_running_child.kill()?;
         let (child, result) = start_desktop(config, self.skip_assets)?;
         self.currently_running_child = child;
         Ok(result)
     }
 }
+
+struct RAIIChild(Child);
+
+impl Drop for RAIIChild {
+    fn drop(&mut self) {
+        let _ = self.0.kill();
+    }
+}

+ 0 - 7
packages/web/src/lib.rs

@@ -252,13 +252,6 @@ pub async fn run_with_props<T: 'static>(root: fn(Scope<T>) -> Element, root_prop
             pin_mut!(work);
 
             #[cfg(all(feature = "hot_reload", debug_assertions))]
-            // futures_util::select! {
-            //     _ = work => (None, None),
-            //     new_template = hotreload_rx.next() => {
-            //         (None, new_template)
-            //     }
-            //     evt = rx.next() =>
-            // }
             match select(work, select(hotreload_rx.next(), rx.next())).await {
                 Either::Left((_, _)) => (None, None),
                 Either::Right((Either::Left((new_template, _)), _)) => (None, new_template),