-
-
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 and objects #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
gh-135607: Remove null checking of weakref list in dealloc of extension modules and objects #135614
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.
Objects/moduleobject.c
Outdated
@@ -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 |
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.) |
I recommend using a macro that restores the NULL check for nogil builds. ISTM there is no reason to hurt the nogil builds, even slightly. |
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.
Ok I haven't comment all occurrences, but avoid re-casting to PyObject*
something that has been cast to the object type. Just re-use the original argument.
62d1912
to
b636d07
Compare
a344210
to
fd1fa03
Compare
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.
LGTM
Co-authored-by: Victor Stinner <vstinner@python.org>
Thanks @xuantengh for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
Sorry, @xuantengh and @kumaraditya303, I could not cleanly backport this to
|
Sorry, @xuantengh and @kumaraditya303, I could not cleanly backport this to
|
…xtension modules and objects (python#135614) Co-authored-by: Kumar Aditya <kumaraditya@python.org> Co-authored-by: Victor Stinner <vstinner@python.org>
GH-136119 is a backport of this pull request to the 3.14 branch. |
…xtension modules and objects (python#135614) Co-authored-by: Kumar Aditya <kumaraditya@python.org> Co-authored-by: Victor Stinner <vstinner@python.org> (cherry picked from commit b1056c2)
…extension modules and objects (#135614) (#136119) gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614) (cherry picked from commit b1056c2) Co-authored-by: Xuanteng Huang <44627253+xuantengh@users.noreply.github.com> Co-authored-by: Kumar Aditya <kumaraditya@python.org>
GH-136126 is a backport of this pull request to the 3.13 branch. |
…xtension modules and objects (python#135614) Co-authored-by: Kumar Aditya <kumaraditya@python.org> Co-authored-by: Victor Stinner <vstinner@python.org> (cherry picked from commit b1056c2)
…extension modules and objects (#135614) (#136126) gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614) (cherry picked from commit b1056c2) Co-authored-by: Xuanteng Huang <44627253+xuantengh@users.noreply.github.com> Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Thanks @xuantengh. I backported your change manually to 3.13 and 3.14 to fix merge conflicts. |
Thanks for your efforts! |
Uh oh!
There was an error while loading. Please reload this page.