diff --git a/src/clipboard/x11.rs b/src/clipboard/x11.rs index 3ad74b384..5dc2a63a0 100644 --- a/src/clipboard/x11.rs +++ b/src/clipboard/x11.rs @@ -16,6 +16,10 @@ use xcb_util; use clipboard::{ClipboardImpl, Paste}; +#[derive(Debug, Fail)] +#[fail(display = "ClipTerminated")] +struct ClipTerminated; + #[derive(Debug)] enum ClipRequest { /// tell the system that we want to set (or clear) the @@ -306,7 +310,7 @@ impl Inner { fn process_one_cliprequest(&mut self, request: ClipRequest) -> Result<(), Error> { match request { - ClipRequest::Terminate => bail!("requested termination of clip thread"), + ClipRequest::Terminate => return Err(ClipTerminated.into()), ClipRequest::SetClipboard(clip) => { self.owned = clip; self.update_owner(); @@ -365,10 +369,14 @@ impl Inner { } if event.token() == Token(1) { match self.process_receiver() { - Err(err) => { - eprintln!("clipboard: {:?}", err); - return; - } + // No need to print the error trace if we were shutdown gracefully + Err(err) => match err.downcast_ref::() { + Some(_) => return, + _ => { + eprintln!("clipboard: {:?}", err); + return; + } + }, _ => (), } self.conn.flush(); @@ -388,8 +396,7 @@ pub struct Clipboard { sender: MioSender, receiver: Receiver, /// This isn't really dead; we're keeping it alive until we Drop. - #[allow(dead_code)] - clip_thread: JoinHandle<()>, + clip_thread: Option>, } impl ClipboardImpl for Clipboard { @@ -413,7 +420,7 @@ impl ClipboardImpl for Clipboard { Ok(Self { sender: sender_clip, receiver: receiver_paste, - clip_thread, + clip_thread: Some(clip_thread), }) } @@ -445,5 +452,11 @@ impl ClipboardImpl for Clipboard { impl Drop for Clipboard { fn drop(&mut self) { self.sender.send(ClipRequest::Terminate).ok(); + match self.clip_thread.take() { + Some(thread) => { + thread.join().ok(); + } + None => {} + } } } diff --git a/src/main.rs b/src/main.rs index 269ad8283..0bd14c393 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,7 @@ use glium::glutin::WindowId; use mio::{Events, Poll, PollOpt, Ready, Token}; use mio::unix::EventedFd; use mio_extras::channel::{channel as mio_channel, Receiver as MioReceiver}; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::env; use std::ffi::CStr; use std::os::unix::io::AsRawFd; @@ -88,6 +88,7 @@ enum PtyMsg { AddPty(WindowId, RawFd), #[allow(dead_code)] DelPty(WindowId, RawFd), + Terminate, } fn run_pty_thread(mut wakeup: Wakeup, receiver: MioReceiver) -> Result<(), Error> { @@ -125,6 +126,10 @@ fn run_pty_thread(mut wakeup: Wakeup, receiver: MioReceiver) -> Result<( match receiver.try_recv() { Err(TryRecvError::Empty) => {} Err(err) => bail!("error receiving PtyMsg {:?}", err), + Ok(PtyMsg::Terminate) => { + eprintln!("pty thread: Terminate"); + return Ok(()); + } Ok(PtyMsg::AddPty(window_id, fd)) => { let token = Token(next_token_id); poll.register( @@ -138,6 +143,7 @@ fn run_pty_thread(mut wakeup: Wakeup, receiver: MioReceiver) -> Result<( next_token_id += 1; } Ok(PtyMsg::DelPty(_window_id, fd)) => { + eprintln!("DelPty {:?} {}", _window_id, fd); poll.deregister(&EventedFd(&fd))?; } } @@ -155,11 +161,11 @@ fn run_pty_thread(mut wakeup: Wakeup, receiver: MioReceiver) -> Result<( } } -fn start_pty_thread(wakeup: Wakeup, receiver: MioReceiver) { +fn start_pty_thread(wakeup: Wakeup, receiver: MioReceiver) -> std::thread::JoinHandle<()> { thread::spawn(move || match run_pty_thread(wakeup, receiver) { Ok(_) => {} - Err(err) => eprintln!("pty thread: {:?}", err), - }); + Err(err) => eprintln!("pty thread returned error {:?}", err), + }) } fn run_glium( @@ -179,7 +185,7 @@ fn run_glium( sigchld::activate(wakeup.clone())?; let (pty_sender, pty_receiver) = mio_channel(); - start_pty_thread(wakeup.clone(), pty_receiver); + let pty_thread = start_pty_thread(wakeup.clone(), pty_receiver); let master_fd = master.as_raw_fd(); @@ -207,14 +213,14 @@ fn run_glium( use glium::glutin::ControlFlow::{Break, Continue}; use glium::glutin::Event; - let mut dead_windows = Vec::new(); + let mut dead_windows = HashSet::new(); let result = match event { Event::WindowEvent { window_id, .. } => match windows_by_id.get_mut(&window_id) { Some(window) => match window.dispatch_event(event) { Ok(_) => Continue, Err(err) => match err.downcast_ref::() { Some(_) => { - dead_windows.push(window_id.clone()); + dead_windows.insert(window_id.clone()); Continue } _ => { @@ -239,7 +245,7 @@ fn run_glium( Ok(_) => {} Err(err) => { eprintln!("pty finished: {:?}", err); - dead_windows.push(window_id.clone()); + dead_windows.insert(window_id.clone()); } } }, @@ -260,7 +266,12 @@ fn run_glium( match result { Continue => { for window_id in dead_windows { - // TODO: DelPty + // TODO: DelPty. Doing this here spews errors + /* + pty_sender + .send(PtyMsg::DelPty(window_id, master_fd)) + .unwrap(); + */ windows_by_id.remove(&window_id); } @@ -275,6 +286,9 @@ fn run_glium( } }); + pty_sender.send(PtyMsg::Terminate)?; + pty_thread.join().ok(); + Ok(()) }