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 9 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
19 changes: 19 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -7295,6 +7295,25 @@ def test_update_type_cache(self):
""")
script_helper.assert_python_ok('-c', script)

def test_static_type_at_shutdown(self):
# gh-132413
script = textwrap.dedent("""
import _datetime
timedelta = _datetime.timedelta
def gen():
try:
yield
finally:
# sys.modules is empty
_datetime.timedelta(days=1)
timedelta(days=1)
it = gen()
next(it)
""")
script_helper.assert_python_ok('-c', script)


def load_tests(loader, standard_tests, pattern):
standard_tests.addTest(ZoneInfoCompleteTest())
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.
30 changes: 28 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,10 @@ static PyObject*
finalize_remove_modules(PyObject *modules, int verbose)
{
PyObject *weaklist = PyList_New(0);
PyObject *weak_ext = PyList_New(0); // for extending the weaklist
if (weak_ext == NULL) {
Py_CLEAR(weaklist);
}
if (weaklist == NULL) {
PyErr_FormatUnraisable("Exception ignored while removing modules");
}
Expand All @@ -1554,8 +1558,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 +1625,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