more shutdown cleanup
We don't print so many stack traces on shutdown, and we now properly join our threads. Need to improve how we handle DelPty though.
This commit is contained in:
1 parent
e1001ca289
commit
377135a71c
2 files changed
+44
-17
No files matched your search
+21
-8
@@ -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::<ClipTerminated>() {
|
||||
Some(_) => return,
|
||||
_ => {
|
||||
eprintln!("clipboard: {:?}", err);
|
||||
return;
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
self.conn.flush();
|
||||
@@ -388,8 +396,7 @@ pub struct Clipboard {
|
||||
sender: MioSender<ClipRequest>,
|
||||
receiver: Receiver<Paste>,
|
||||
/// This isn't really dead; we're keeping it alive until we Drop.
|
||||
#[allow(dead_code)]
|
||||
clip_thread: JoinHandle<()>,
|
||||
clip_thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
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 => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
-9
@@ -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<PtyMsg>) -> Result<(), Error> {
|
||||
@@ -125,6 +126,10 @@ fn run_pty_thread(mut wakeup: Wakeup, receiver: MioReceiver<PtyMsg>) -> 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<PtyMsg>) -> 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<PtyMsg>) -> Result<(
|
||||
}
|
||||
}
|
||||
|
||||
fn start_pty_thread(wakeup: Wakeup, receiver: MioReceiver<PtyMsg>) {
|
||||
fn start_pty_thread(wakeup: Wakeup, receiver: MioReceiver<PtyMsg>) -> 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::<gliumwindows::SessionTerminated>() {
|
||||
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(())
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user