Repository navigation
mgr/PyModule: do not lock in get_name(), get_error_string() - #60261
Merged
Merged
Conversation
MaxKellermann
force-pushed
the
PyModule_no_lock
branch
from
October 14, 2024 14:43
5b655f8 to
7c193d4
Compare
Member
Author
|
No reply for nearly two months for something that is clearly a trivial bug being fixed. |
Contributor
|
@MaxKellermann error_string is not const. Of course, you are correct that taking the lock and then returning a const ref is nonsense -- should be returned by value. |
athanatos
requested changes
Dec 9, 2024
athanatos
left a comment
Contributor
There was a problem hiding this comment.
error_string is not const and is mutated under the lock in fault(). Add a commit changing get_error_string() to return a value. The get_name() change looks right.
The way get_name() used the lock is completely wrong: in the protected code block, it do not access anything that needs to be protected. All it effectively does is return the `this` pointer with an offset to the `module_name` field; but it does not actually access the field. The actual access is done by the caller, after the mutex has been unlocked already. But anyway, `module_name` does not need any protection. It is `const` and thus cannot be modified by other threads. So instead of fixing the return value (removing the `&`), let's just remove the lock. Signed-off-by: Max Kellermann <[email protected]>
The explanation from the previous commit about get_name() applies here as well; but other than `module_name`, the `error_string` field is mutable and does indeed need protection. But still, returning a reference is wrong, because the reference does not need protection; the pointed-to value does. This can be fixed easily by making the method return a value instead of a reference. The copy is made while holding the lock. Signed-off-by: Max Kellermann <[email protected]>
MaxKellermann
force-pushed
the
PyModule_no_lock
branch
from
December 9, 2024 07:26
7c193d4 to
92aa60e
Compare
Member
Author
|
@athanatos you're right about |
Member
Author
|
@SrinivasaBharath, results of your testing a month ago? |
Collaborator
|
Can one of the admins verify this patch? |
Contributor
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.
The way these methods used the lock is completely wrong: in the protected code block, they do not access anything that needs to be protected. All they effectively do is return the
thispointer with an offset to themodule_nameorerror_stringfield; but they do not actually access these fields. The actual access is done by the caller, after the mutex has been unlocked already.But anyway, these fields do not need any protection. Both fields are
constand thus cannot be modified by other threads. So instead of fixing the return value (removing the&), let's just remove the lock.Checklist