]> git.lizzy.rs Git - rust.git/commitdiff
Fix shutdown behavoir of main cargo-watch thread.
authorEmil Lauridsen <mine809@gmail.com>
Fri, 27 Dec 2019 11:42:18 +0000 (12:42 +0100)
committerEmil Lauridsen <mine809@gmail.com>
Fri, 27 Dec 2019 11:42:18 +0000 (12:42 +0100)
Even though this didn't error, it became clear to me that it was closing
the wrong channel, resulting in the child thread never finishing.

crates/ra_cargo_watch/src/lib.rs

index d17edd77507e3507d5ffd19faab2d26ad2621b37..11b624abfe1fd6d9457e381471f65968209c461f 100644 (file)
@@ -36,7 +36,7 @@ pub struct CheckOptions {
 #[derive(Debug)]
 pub struct CheckWatcher {
     pub task_recv: Receiver<CheckTask>,
-    pub cmd_send: Sender<CheckCommand>,
+    pub cmd_send: Option<Sender<CheckCommand>>,
     pub shared: Arc<RwLock<CheckWatcherSharedState>>,
     handle: Option<JoinHandle<()>>,
 }
@@ -53,23 +53,24 @@ pub fn new(options: &CheckOptions, workspace_root: PathBuf) -> CheckWatcher {
             let mut check = CheckWatcherState::new(options, workspace_root, shared_);
             check.run(&task_send, &cmd_recv);
         });
-        CheckWatcher { task_recv, cmd_send, handle: Some(handle), shared }
+        CheckWatcher { task_recv, cmd_send: Some(cmd_send), handle: Some(handle), shared }
     }
 
     /// Schedule a re-start of the cargo check worker.
     pub fn update(&self) {
-        self.cmd_send.send(CheckCommand::Update).unwrap();
+        if let Some(cmd_send) = &self.cmd_send {
+            cmd_send.send(CheckCommand::Update).unwrap();
+        }
     }
 }
 
 impl std::ops::Drop for CheckWatcher {
     fn drop(&mut self) {
         if let Some(handle) = self.handle.take() {
-            // Replace our reciever with dummy one, so we can drop and close the
-            // one actually communicating with the thread
-            let recv = std::mem::replace(&mut self.task_recv, crossbeam_channel::never());
+            // Take the sender out of the option
+            let recv = self.cmd_send.take();
 
-            // Dropping the original reciever finishes the thread loop
+            // Dropping the sender finishes the thread loop
             drop(recv);
 
             // Join the thread, it should finish shortly. We don't really care