Skip to content

Expose PyMutex_IsLocked in the public C API #134009

Open
@hawkinsp

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

ngoldbaum

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.

ZeroIntensity

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);
}
makslevental

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...)

hawkinsp

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.

colesbury

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.

colesbury

colesbury commented on May 14, 2025

@colesbury
Contributor

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

changed the title [-]Expose `PyMutex_IsLocked` in the public API for free-threading[/-] [+]Expose `PyMutex_IsLocked` in the public API[/+] on May 15, 2025
changed the title [-]Expose `PyMutex_IsLocked` in the public API[/-] [+]Expose `PyMutex_IsLocked` in the public C API[/+] on May 15, 2025
added a commit that references this issue on May 20, 2025

pythongh-134009: Expose `PyMutex_IsLocked` in the public C API

2 remaining items

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

      Expose `PyMutex_IsLocked` in the public C API · Issue #134009 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant