Avoid locking when printing (#333)

* Avoid locking when printing

It is not obvious that attempting to print the
value of a struct could cause a deadlock. To avoid
this, this patch does not lock the mutex when generating
a debug representation of the h2 struct.

* Use try_lock
This commit is contained in:
Carl Lerche
2018-11-29 21:50:53 -08:00
committed by Sean McArthur
parent e656c42353
commit 8387355e1b

View File

@@ -1043,7 +1043,9 @@ impl OpaqueStreamRef {
impl fmt::Debug for OpaqueStreamRef {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.inner.lock() {
use std::sync::TryLockError::*;
match self.inner.try_lock() {
Ok(me) => {
let stream = &me.store[self.key];
fmt.debug_struct("OpaqueStreamRef")
@@ -1051,9 +1053,16 @@ impl fmt::Debug for OpaqueStreamRef {
.field("ref_count", &stream.ref_count)
.finish()
},
Err(_poisoned) => fmt.debug_struct("OpaqueStreamRef")
.field("inner", &"<Poisoned>")
.finish(),
Err(Poisoned(_)) => {
fmt.debug_struct("OpaqueStreamRef")
.field("inner", &"<Poisoned>")
.finish()
}
Err(WouldBlock) => {
fmt.debug_struct("OpaqueStreamRef")
.field("inner", &"<Locked>")
.finish()
}
}
}
}