Skip to content

gh-132413: Clear weakref to _datetime after modules are finalized #136152

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

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
GC-untrack/track
  • Loading branch information
neonene authored Jul 1, 2025
commit 4b1b45c44da356fa36585814e4b58d8d44c8a75b
22 changes: 9 additions & 13 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1554,18 +1554,14 @@ finalize_remove_modules(PyObject *modules, int verbose)
if (weaklist != NULL) { \
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
if (wr) { \
PyObject *tup; \
if (Py_REFCNT(wr) > 1) { \
/* gh-132413: When the weakref is already used
* elsewhere, keep the referenced module alive
* until finalize_modules_clear_weaklist() finishes.
*/ \
Py_INCREF(mod); \
tup = PyTuple_Pack(3, name, wr, Py_True); \
} \
else { \
tup = PyTuple_Pack(3, name, wr, Py_False); \
/* gh-132413: When the weakref is already used elsewhere,
* finalize_modules_clear_weaklist() rather than the GC
* should clear the referenced module since the GC tries
* to clear the wrakref first. */ \
_PyObject_GC_UNTRACK(mod); \
} \
PyObject *tup = PyTuple_Pack(2, name, wr); \
if (!tup || PyList_Append(weaklist, tup) < 0) { \
PyErr_FormatUnraisable("Exception ignored while removing modules"); \
} \
Expand Down Expand Up @@ -1668,6 +1664,9 @@ finalize_modules_clear_weaklist(PyInterpreterState *interp,
continue;
}
assert(PyModule_Check(mod));
if (!_PyObject_GC_IS_TRACKED(mod)) {
_PyObject_GC_TRACK(mod);
}
PyObject *dict = _PyModule_GetDict(mod); // borrowed reference
if (dict == interp->builtins || dict == interp->sysdict) {
Py_DECREF(mod);
Expand All @@ -1678,9 +1677,6 @@ finalize_modules_clear_weaklist(PyInterpreterState *interp,
}
_PyModule_Clear(mod);
Py_DECREF(mod);
if (PyTuple_GET_ITEM(tup, 2) == Py_True) {
Py_DECREF(mod);
}
}
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy