Bug 1410702 - P1: Handle errors in send_recv! r=kinetik

Connection handling code wasn't handling errors from receive()
properly. Attempting to unwrap on an Err was causing a panic.

MozReview-Commit-ID: GURe3FHPbjT

--HG--
extra : rebase_source : 664bf389020feb4a12f43422351c20ab7e804e30
This commit is contained in:
Dan Glastonbury 2017-10-25 14:53:03 +10:00
parent 443416f881
commit 4f66ebac06
4 changed files with 30 additions and 17 deletions

View File

@ -9,6 +9,7 @@ use mio::unix::EventedFd;
use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use std::collections::VecDeque;
use std::error::Error;
use std::fmt::Debug;
use std::io::{self, Read};
use std::os::unix::io::{AsRawFd, RawFd};
@ -126,7 +127,7 @@ impl Connection {
Ok(Async::NotReady) => bail!("Socket should be blocking."),
// TODO: Handle dropped message.
// Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => panic!("wouldblock"),
_ => bail!("socket write"),
Err(e) => bail!("stream recv_buf_fd returned: {}", e.description()),
}
}
}

View File

@ -243,7 +243,5 @@ pub enum ClientMessage {
StreamDataCallback(isize, usize),
StreamStateCallback(ffi::cubeb_state),
ContextError(ffi::cubeb_error_code),
StreamError, /*(Error)*/
ClientError /*(Error)*/
Error(ffi::cubeb_error_code)
}

View File

@ -32,21 +32,35 @@ macro_rules! send_recv {
}
});
(__recv $conn:expr, $rmsg:ident) => ({
let r = $conn.receive().unwrap();
if let ClientMessage::$rmsg = r {
Ok(())
} else {
debug!("receive error - got={:?}", r);
match $conn.receive() {
Ok(ClientMessage::$rmsg) => Ok(()),
// Macro can see ErrorCode but not Error? I don't understand.
// ::cubeb_core::Error is fragile but this isn't general purpose code.
Ok(ClientMessage::Error(e)) => Err(unsafe { ::cubeb_core::Error::from_raw(e) }),
Ok(m) => {
debug!("receive unexpected message - got={:?}", m);
Err(ErrorCode::Error.into())
},
Err(e) => {
debug!("receive error - got={:?}", e);
Err(ErrorCode::Error.into())
},
}
});
(__recv $conn:expr, $rmsg:ident __result) => ({
let r = $conn.receive().unwrap();
if let ClientMessage::$rmsg(v) = r {
Ok(v)
} else {
debug!("receive error - got={:?}", r);
match $conn.receive() {
Ok(ClientMessage::$rmsg(v)) => Ok(v),
// Macro can see ErrorCode but not Error? I don't understand.
// ::cubeb_core::Error is fragile but this isn't general purpose code.
Ok(ClientMessage::Error(e)) => Err(unsafe { ::cubeb_core::Error::from_raw(e) }),
Ok(m) => {
debug!("receive unexpected message - got={:?}", m);
Err(ErrorCode::Error.into())
},
Err(e) => {
debug!("receive error - got={:?}", e);
Err(ErrorCode::Error.into())
},
}
})
}

View File

@ -763,7 +763,7 @@ pub fn run(running: Arc<AtomicBool>) -> Result<()> {
}
fn error(error: cubeb::Error) -> ClientMessage {
ClientMessage::ContextError(error.raw_code())
ClientMessage::Error(error.raw_code())
}
struct ServerWrapper {