Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic when the NativeSslStream lock is poisoned #429

Merged
merged 1 commit into from
May 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/rustup-utils/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,39 +202,66 @@ pub fn download_file<P: AsRef<Path>>(url: hyper::Url,
#[derive(Clone)]
struct NativeSslStream<T>(Arc<Mutex<native_tls::TlsStream<T>>>);

#[derive(Debug)]
struct NativeSslPoisonError;

impl ::std::error::Error for NativeSslPoisonError {
fn description(&self) -> &str { "mutex poisoned during TLS operation" }
}

impl ::std::fmt::Display for NativeSslPoisonError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
f.write_str(::std::error::Error::description(self))
}
}

impl<T> NetworkStream for NativeSslStream<T>
where T: NetworkStream
{
fn peer_addr(&mut self) -> IoResult<SocketAddr> {
self.0.lock().expect("").get_mut().peer_addr()
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.get_mut().peer_addr())
}
fn set_read_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
self.0.lock().expect("").get_ref().set_read_timeout(dur)
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|t| t.get_ref().set_read_timeout(dur))
}
fn set_write_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
self.0.lock().expect("").get_ref().set_read_timeout(dur)
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|t| t.get_ref().set_write_timeout(dur))
}
fn close(&mut self, how: Shutdown) -> IoResult<()> {
self.0.lock().expect("").get_mut().close(how)
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.get_mut().close(how))
}
}

impl<T> Read for NativeSslStream<T>
where T: Read + Write
{
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
self.0.lock().expect("").read(buf)
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.read(buf))
}
}

impl<T> Write for NativeSslStream<T>
where T: Read + Write
{
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
self.0.lock().expect("").write(buf)
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.write(buf))
}
fn flush(&mut self) -> IoResult<()> {
self.0.lock().expect("").flush()
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.flush())
}
}

Expand Down