-
Notifications
You must be signed in to change notification settings - Fork 805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add a MutexExt trait to avoid deadlocks using Mutex::lock() #4934
Conversation
aa42fca
to
c4de919
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looking good! One possible optimization opportunity for us to take, maybe?
&self, | ||
_py: Python<'_>, | ||
) -> std::sync::LockResult<std::sync::MutexGuard<'_, T>> { | ||
// SAFETY: detach from the runtime right before a possibly blocking call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of unconditionally detaching, is it worth calling self.try_lock()
first, and returning eagerly in all cases except where it would block? (And then if it would block, continue to the code below.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it can't hurt to avoid detaching and reattaching if it's possible. I'll go ahead and do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM too with one tiny code style suggestion, thanks 👍
Co-authored-by: David Hewitt <[email protected]>
Looks like a network error. One more try… |
Fixes #4771.
I couldn't find any good uses for this inside PyO3 itself, but we know of at least two spots in rust-numpy this would be useful.