Skip to content

Commit e2f0dc8

Browse files
committed
Don't SSLClose a panicked connection
Once a `Connection` has panicked in I/O it's effectively poisoned and we shouldn't come back to it in the destructor, so skip `SSLClose` in this case.
1 parent 4730e30 commit e2f0dc8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

security-framework/src/secure_transport.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ impl SslContext {
688688
stream: stream,
689689
err: None,
690690
panic: None,
691+
panicked: false,
691692
};
692693
let stream = Box::into_raw(Box::new(stream));
693694
let ret = SSLSetConnection(self.0, stream as *mut _);
@@ -709,6 +710,7 @@ struct Connection<S> {
709710
stream: S,
710711
err: Option<io::Error>,
711712
panic: Option<Box<Any + Send>>,
713+
panicked: bool,
712714
}
713715

714716
// the logic here is based off of libcurl's
@@ -746,6 +748,7 @@ unsafe extern "C" fn read_func<S: Read>(connection: SSLConnectionRef,
746748
Err(e) => {
747749
ret = errSecIO;
748750
conn.panic = Some(e);
751+
conn.panicked = true;
749752
break;
750753
}
751754
}
@@ -779,6 +782,7 @@ unsafe extern "C" fn write_func<S: Write>(connection: SSLConnectionRef,
779782
Err(e) => {
780783
ret = errSecIO;
781784
conn.panic = Some(e);
785+
conn.panicked = true;
782786
break;
783787
}
784788
}
@@ -806,7 +810,12 @@ impl<S: fmt::Debug> fmt::Debug for SslStream<S> {
806810
impl<S> Drop for SslStream<S> {
807811
fn drop(&mut self) {
808812
unsafe {
809-
SSLClose(self.ctx.0);
813+
// if read/write previously panicked then it's likely that this
814+
// destructor is being run as part of that propagation, and in that
815+
// case let's avoid more I/O as part of `SSLClose`
816+
if !self.connection().panicked {
817+
SSLClose(self.ctx.0);
818+
}
810819

811820
let mut conn = ptr::null();
812821
let ret = SSLGetConnection(self.ctx.0, &mut conn);

0 commit comments

Comments
 (0)