Skip to content

mgr/PyModule: do not lock in get_name(), get_error_string() - #60261

Merged
SrinivasaBharath merged 2 commits into
ceph:mainfrom
MaxKellermann:PyModule_no_lock
Jan 13, 2025
Merged

SrinivasaBharath merged 2 commits into
ceph:mainfrom
MaxKellermann:PyModule_no_lock

Conversation

@MaxKellermann

Copy link
Copy Markdown
Member

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 this pointer with an offset to the module_name or error_string field; 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 const and thus cannot be modified by other threads. So instead of fixing the return value (removing the &), let's just remove the lock.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)

@MaxKellermann

Copy link
Copy Markdown
Member Author

No reply for nearly two months for something that is clearly a trivial bug being fixed.

@athanatos

Copy link
Copy Markdown
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 athanatos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

@athanatos you're right about error_string - I have splitted the PR into two commits, each addressing one of the two methods.

@athanatos athanatos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs qa run.

@MaxKellermann

Copy link
Copy Markdown
Member Author

@SrinivasaBharath, results of your testing a month ago?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@Naveenaidu

Copy link
Copy Markdown
Contributor

@SrinivasaBharath
SrinivasaBharath merged commit fc187f4 into ceph:main Jan 13, 2025
@MaxKellermann
MaxKellermann deleted the PyModule_no_lock branch January 13, 2025 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants