-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
Misc/NEWS.d/next/Core_and_Builtins/2025-06-17-22-34-58.gh-issue-135607.ucsLVu.rst
Outdated
Show resolved
Hide resolved
Misc/NEWS.d/next/Core_and_Builtins/2025-06-17-22-34-58.gh-issue-135607.ucsLVu.rst
Outdated
Show resolved
Hide resolved
9a6819c
to
68528e6
Compare
Lib/test/test_weakref.py
Outdated
@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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments:
- I don't think we need to add test cases for this kind of thing.
- 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
So next we should:
cpython/Modules/_threadmodule.c Lines 1534 to 1542 in 725da50
cpython/Modules/_threadmodule.c Lines 1364 to 1370 in 725da50
|
bc28bd7
to
9024079
Compare
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 |
This reverts commit 9024079.
For objects without weakref, the "additional cost" would be: cpython/Objects/weakrefobject.c Lines 1010 to 1016 in 140731f
which IMHO is negligible? |
22860cd
to
54fa61e
Compare
Ok, for clarification, when do you want free-threading changes to include tests? I think that a lot of them could go under the "trivial" umbrella, but we've still added tests for them in the past. (It makes sense to me to omit tests now, because we're changing lots of different parts of the codebase, but I don't totally see why it wasn't a good idea for the original one-line change.) |
Uh oh!
There was an error while loading. Please reload this page.