From 8387355e1bb697256862c58a84b6cd581b1629bf Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 29 Nov 2018 21:50:53 -0800 Subject: [PATCH] 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 --- src/proto/streams/streams.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/proto/streams/streams.rs b/src/proto/streams/streams.rs index cb8a6d5..cb02c8b 100644 --- a/src/proto/streams/streams.rs +++ b/src/proto/streams/streams.rs @@ -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", &"") - .finish(), + Err(Poisoned(_)) => { + fmt.debug_struct("OpaqueStreamRef") + .field("inner", &"") + .finish() + } + Err(WouldBlock) => { + fmt.debug_struct("OpaqueStreamRef") + .field("inner", &"") + .finish() + } } } }