Skip to content
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

Make refleak checking thread-safe without the GIL #117439

Closed
colesbury opened this issue Apr 1, 2024 · 0 comments
Closed

Make refleak checking thread-safe without the GIL #117439

colesbury opened this issue Apr 1, 2024 · 0 comments
Labels
topic-free-threading type-feature A feature request or enhancement

Comments

@colesbury
Copy link
Contributor

colesbury commented Apr 1, 2024

Feature or enhancement

The refleak checking relies on per-interpreter "total" refcount tracking. It uses non-atomic operations and is not thread-safe without the GIL.

In the free-threaded build, I think we should primarily track counts in PyThreadState and occasionally aggregate the results into the per-interpreter total refcount using atomic operations.

See:

cpython/Objects/object.c

Lines 72 to 91 in 9dae05e

# define REFTOTAL(interp) \
interp->object_state.reftotal
static inline void
reftotal_increment(PyInterpreterState *interp)
{
REFTOTAL(interp)++;
}
static inline void
reftotal_decrement(PyInterpreterState *interp)
{
REFTOTAL(interp)--;
}
static inline void
reftotal_add(PyInterpreterState *interp, Py_ssize_t n)
{
REFTOTAL(interp) += n;
}

There is also the legacy _Py_RefTotal, but that's just preserved for ABI compatibility. I don't think we have to do anything with that.

Linked PRs

@colesbury colesbury added type-feature A feature request or enhancement topic-free-threading labels Apr 1, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Apr 2, 2024
This keeps track of the per-thread total reference count operations in
PyThreadState in the free-threaded builds. The count is merged into the
interpreter's total when the thread exits.
colesbury added a commit to colesbury/cpython that referenced this issue Apr 2, 2024
This keeps track of the per-thread total reference count operations in
PyThreadState in the free-threaded builds. The count is merged into the
interpreter's total when the thread exits.
colesbury added a commit to colesbury/cpython that referenced this issue Apr 2, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Apr 2, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Apr 4, 2024
colesbury added a commit that referenced this issue Apr 8, 2024
This keeps track of the per-thread total reference count operations in
PyThreadState in the free-threaded builds. The count is merged into the
interpreter's total when the thread exits.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…ython#117469)

This keeps track of the per-thread total reference count operations in
PyThreadState in the free-threaded builds. The count is merged into the
interpreter's total when the thread exits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-free-threading type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant