Skip to content

gh-135607: Remove null checking of weakref list in module dealloc #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 8 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 from d375d34 to 9a6819c Compare June 18, 2025 07:35
@xuantengh xuantengh force-pushed the remove-weakref-null-checking branch 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

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.

5 participants