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:
		
				
					committed by
					
						 Sean McArthur
						Sean McArthur
					
				
			
			
				
	
			
			
			
						parent
						
							e656c42353
						
					
				
				
					commit
					8387355e1b
				
			| @@ -1043,7 +1043,9 @@ impl OpaqueStreamRef { | |||||||
|  |  | ||||||
| impl fmt::Debug for OpaqueStreamRef { | impl fmt::Debug for OpaqueStreamRef { | ||||||
|     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||||||
|         match self.inner.lock() { |         use std::sync::TryLockError::*; | ||||||
|  |  | ||||||
|  |         match self.inner.try_lock() { | ||||||
|             Ok(me) => { |             Ok(me) => { | ||||||
|                 let stream = &me.store[self.key]; |                 let stream = &me.store[self.key]; | ||||||
|                 fmt.debug_struct("OpaqueStreamRef") |                 fmt.debug_struct("OpaqueStreamRef") | ||||||
| @@ -1051,9 +1053,16 @@ impl fmt::Debug for OpaqueStreamRef { | |||||||
|                     .field("ref_count", &stream.ref_count) |                     .field("ref_count", &stream.ref_count) | ||||||
|                     .finish() |                     .finish() | ||||||
|             }, |             }, | ||||||
|             Err(_poisoned) => fmt.debug_struct("OpaqueStreamRef") |             Err(Poisoned(_)) => { | ||||||
|  |                 fmt.debug_struct("OpaqueStreamRef") | ||||||
|                     .field("inner", &"<Poisoned>") |                     .field("inner", &"<Poisoned>") | ||||||
|                 .finish(), |                     .finish() | ||||||
|  |             } | ||||||
|  |             Err(WouldBlock) => { | ||||||
|  |                 fmt.debug_struct("OpaqueStreamRef") | ||||||
|  |                     .field("inner", &"<Locked>") | ||||||
|  |                     .finish() | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user