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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,29 @@ def test_ast_fini(self):
""")
assert_python_ok("-c", code)

def test_weakref_to_module_at_shutdown(self):
# gh-132413: Weakref gets cleared before modules are finalized
code = textwrap.dedent("""
import os # any module other than sys

def gen():
import weakref
wr = weakref.ref(os)
try:
yield
finally:
print(
os is not None,
os is wr()
)

it = gen()
next(it)
# quirk: Shutdown starts, then the finally block above follows
""")
res = assert_python_ok('-c', code)
self.assertEqual(res.out.rstrip(), b'True True')


def setUpModule():
global enabled, debug
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash in C version of :mod:`datetime` when used during interpreter shutdown.
33 changes: 31 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,13 @@ static PyObject*
finalize_remove_modules(PyObject *modules, int verbose)
{
PyObject *weaklist = PyList_New(0);
PyObject *weak_ext = NULL; // for extending the weaklist
if (weaklist != NULL) {
weak_ext = PyList_New(0);
if (weak_ext == NULL) {
Py_CLEAR(weaklist);
}
}
if (weaklist == NULL) {
PyErr_FormatUnraisable("Exception ignored while removing modules");
}
Expand All @@ -1554,8 +1561,22 @@ finalize_remove_modules(PyObject *modules, int verbose)
if (weaklist != NULL) { \
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
if (wr) { \
PyObject *tup = PyTuple_Pack(2, name, wr); \
if (!tup || PyList_Append(weaklist, tup) < 0) { \
PyObject *list; \
PyObject *tup; \
if (Py_REFCNT(wr) > 1) { \
/* 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. The weaklist requires the
* order in which such modules are cleared first. */ \
tup = PyTuple_Pack(3, name, wr, mod); \
list = weak_ext; \
} \
else { \
tup = PyTuple_Pack(2, name, wr); \
list = weaklist; \
} \
if (!tup || PyList_Append(list, tup) < 0) { \
PyErr_FormatUnraisable("Exception ignored while removing modules"); \
} \
Py_XDECREF(tup); \
Expand Down Expand Up @@ -1607,6 +1628,14 @@ finalize_remove_modules(PyObject *modules, int verbose)
Py_DECREF(iterator);
}
}

if (weaklist != NULL) {
if (PyList_Extend(weaklist, weak_ext) < 0) {
PyErr_FormatUnraisable("Exception ignored while removing modules");
}
Py_DECREF(weak_ext);
}

#undef CLEAR_MODULE
#undef STORE_MODULE_WEAKREF

Expand Down
Loading