Add lock! macro to handle mutex poisoning gracefully#471
Open
crodas wants to merge 1 commit intorust-bitcoin:masterfrom
Open
Add lock! macro to handle mutex poisoning gracefully#471crodas wants to merge 1 commit intorust-bitcoin:masterfrom
crodas wants to merge 1 commit intorust-bitcoin:masterfrom
Conversation
Mutex::lock().unwrap() panics if the mutex is poisoned (when a thread panicked while holding the lock). In practice, this is rarely the desired behavior - the data may still be valid and propagating panics across threads is usually unhelpful. This adds a lock! macro that recovers from poisoning via unwrap_or_else(|e| e.into_inner()), providing a concise and consistent way to acquire locks throughout the codebase. An alternative would be using parking_lot::Mutex which has no poisoning semantics, but this avoids adding an external dependency.
TheBlueMatt
reviewed
Jan 23, 2026
Member
TheBlueMatt
left a comment
There was a problem hiding this comment.
I'd kinda rather make sure that none of the operations within any of our locks can possibly panic. I believe we're close but you identified one bug where we're holding a mutex for too long.
| // Try to get cached connection | ||
| let conn_opt = { | ||
| let state = self.r#async.lock().unwrap(); | ||
| let state = lock!(self.r#async); |
Member
There was a problem hiding this comment.
I do not believe any of the calls within either lock here can panic at all.
| request: ParsedRequest, | ||
| ) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + Send + 'a>> { | ||
| Box::pin(async move { | ||
| let conn = Arc::clone(&*self.0.lock().unwrap()); |
Member
There was a problem hiding this comment.
ISTM all of the locks for this mutex are only held on one line and used to clone or assign the Arc, I don't believe that can panic?
| }; | ||
|
|
||
| let socket_timeout = *conn.socket_new_requests_timeout.lock().unwrap(); | ||
| let socket_timeout = *lock!(conn.socket_new_requests_timeout); |
Member
There was a problem hiding this comment.
Hmm, good catch I believe this mutex shouldn't be held nearly as long as it is, it should only be required for the next line then should be dropped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mutex::lock().unwrap() panics if the mutex is poisoned (when a thread panicked while holding the lock). In practice, this is rarely the desired behavior - the data may still be valid and propagating panics across threads is usually unhelpful.
This adds a lock! macro that recovers from poisoning via unwrap_or_else(|e| e.into_inner()), providing a concise and consistent way to acquire locks throughout the codebase.
An alternative would be using parking_lot::Mutex which has no poisoning semantics, but this avoids adding an external dependency.