Skip to content

gh-135607: Remove null checking of weakref list in dealloc of extension modules #135614

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
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

xuantengh
Copy link
Contributor

@xuantengh xuantengh commented Jun 17, 2025

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Please add a test case and blurb entry.

@xuantengh xuantengh force-pushed the remove-weakref-null-checking branch 2 times, most recently from 9a6819c to 68528e6 Compare June 18, 2025 08:34
@threading_helper.requires_working_threading()
@unittest.skipUnless(support.Py_GIL_DISABLED, 'only used under free-threaded build')
def test_module_weakref(self):
# gh-135607: Avoid potential races on module weaklist under free-threaded build
Copy link
Member

Choose a reason for hiding this comment

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

This is unrelated to module_dealloc(). I'm not convinced that this test is relevant.

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

A few comments:

  1. I don't think we need to add test cases for this kind of thing.
  2. I think you should do this project wide. There's a bunch of places with this pattern and we don't need 20 separate one line PRs.

There's some project "cost" for adding code, including new tests both in maintenance, readability, and CI time. These sorts of tests are so specific that they are unlikely to catch any future bugs; I don't think they are worthwhile.

@@ -826,8 +826,7 @@ module_dealloc(PyObject *self)
if (verbose && m->md_name) {
PySys_FormatStderr("# destroy %U\n", m->md_name);
}
if (m->md_weaklist != NULL)
PyObject_ClearWeakRefs((PyObject *) m);
PyObject_ClearWeakRefs(self);
Copy link
Contributor

Choose a reason for hiding this comment

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

We can also write:

if (FT_ATOMIC_LOAD_PTR(m->md_weaklist != NULL))
    PyObject_ClearWeakRefs((PyObject *) m);

Which is a less risky change in the sense that it doesn't change the default build at all.

Copy link
Contributor

@kumaraditya303 kumaraditya303 Jun 18, 2025

Choose a reason for hiding this comment

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

I think we should remove the null check for both builds, it is redundant and many modules already doesn't check it like _asyncio and _thread

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine, just make sure it doesn't introduce a perf regression

@xuantengh
Copy link
Contributor Author

xuantengh commented Jun 19, 2025

So next we should:

  1. remove the introduced test
  2. find all patterns with null checks followed by PyObject_ClearWeakRefs, e.g.:

static void
local_dealloc(PyObject *op)
{
localobject *self = localobject_CAST(op);
/* Weakrefs must be invalidated right now, otherwise they can be used
from code called below, which is very dangerous since Py_REFCNT(self) == 0 */
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs(op);
}

static void
localdummy_dealloc(PyObject *op)
{
localdummyobject *self = localdummyobject_CAST(op);
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs(op);
}

@xuantengh xuantengh changed the title gh-135607: Remove null checking of weakref list in module dealloc gh-135607: Remove null checking of weakref list in dealloc of extension modules Jun 19, 2025
@rhettinger
Copy link
Contributor

Is this going to create an additional cost for deallocating every instance of every extension module type even if weakrefs are never used?

Does this need a macro with an #ifdef so as to not impact the performance of a no-gil build?

@xuantengh
Copy link
Contributor Author

Is this going to create an additional cost for deallocating every instance of every extension module type even if weakrefs are never used?

For objects without weakref, the "additional cost" would be:

if (object == NULL
|| !_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))
|| Py_REFCNT(object) != 0)
{
PyErr_BadInternalCall();
return;
}

which IMHO is negligible?

@xuantengh xuantengh force-pushed the remove-weakref-null-checking branch from 22860cd to 54fa61e Compare June 19, 2025 09:30
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.

6 participants