Skip to content

Expose PyMutex_IsLocked in the public C API #134009

Description

@hawkinsp

Feature or enhancement

Proposal:

In my C extension, I wanted to write:

assert(PyMutex_IsLocked(&mutex));

to verify that my own code is maintaining its own locking invariants.

PyMutex_IsLocked exists, but is private:

PyMutex_IsLocked(PyMutex *m)

Could we make it public and document it, please?

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. ngoldbaum commented on May 14, 2025

    @ngoldbaum
    Contributor

    I ran into this when I was writing PyMutex bindings for PyO3 - it was very difficult to test the bindings because the PyMutex API is so limited.

  2. ZeroIntensity commented on May 14, 2025

    @ZeroIntensity
    Member

    Ideally, we should document that it's for use in assertions only. We need to make sure we're not enabling people to write thread-unsafe code like this:

    if (PyMutex_IsLocked(lock)) {
        PyMutex_Unlock(lock);
    }
  3. makslevental commented on May 14, 2025

    @makslevental

    Ideally, we should document that it's for use in assertions only

    maybe something like

    #define ASSERT_PY_MUTEX_IS_LOCKED(mutex, msg) assert(PyMutex_IsLocked(mutex) && msg)

    ?

    but not sure if this y'all's style (i came here from mlir...)

  4. hawkinsp commented on May 14, 2025

    @hawkinsp
    ContributorAuthor

    It's important to expose an API like PyMutex_IsLocked even if you expose some sort of assertion helper, because people want to use their own debugging/printing/logging libraries to handle this sort of failure.

  5. colesbury commented on May 14, 2025

    @colesbury
    Contributor

    Yeah, I just want to echo @hawkinsp comment. I think we should expose PyMutex_IsLocked() and document its intended purposes. I don't think we need to be overly concerned about people misusing it.

  6. colesbury commented on May 14, 2025

    @colesbury
    Contributor

    We should also open a corresponding issue in the C API working group

  7. changed the title [-]Expose `PyMutex_IsLocked` in the public API for free-threading[/-] [+]Expose `PyMutex_IsLocked` in the public API[/+] on May 15, 2025
  8. changed the title [-]Expose `PyMutex_IsLocked` in the public API[/-] [+]Expose `PyMutex_IsLocked` in the public C API[/+] on May 15, 2025
  9. added a commit that references this issue on May 20, 2025
  10. added a commit that references this issue on May 27, 2025
  11. added 2 commits that reference this issue on Jun 24, 2025
  12. added a commit that references this issue on Jul 11, 2025
  13. added a commit that references this issue on Jul 12, 2025
  14. added a commit that references this issue on Jul 13, 2025
  15. added 2 commits that reference this issue on Jul 22, 2025
  16. added a commit that references this issue on Aug 4, 2025
  17. added a commit that references this issue on Aug 19, 2025
  18. added a commit that references this issue on Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions