Skip to content

Expose PyMutex_IsLocked in the public C API #134009

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

Open
hawkinsp opened this issue May 14, 2025 · 6 comments
Open

Expose PyMutex_IsLocked in the public C API #134009

hawkinsp opened this issue May 14, 2025 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-free-threading type-feature A feature request or enhancement

Comments

@hawkinsp
Copy link
Contributor

hawkinsp commented May 14, 2025

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

@ngoldbaum
Copy link
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
Copy link
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
Copy link

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
Copy link
Contributor Author

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
Copy link
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
Copy link
Contributor

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

@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label May 15, 2025
@colesbury colesbury changed the title Expose PyMutex_IsLocked in the public API for free-threading Expose PyMutex_IsLocked in the public API May 15, 2025
@colesbury colesbury changed the title Expose PyMutex_IsLocked in the public API Expose PyMutex_IsLocked in the public C API May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-free-threading type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

6 participants