Skip to content

gh-136421: Fix crash in _datetime when initialized/finalized concurrently #136620

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -7295,6 +7295,31 @@ def test_update_type_cache(self):
""")
script_helper.assert_python_ok('-c', script)

@unittest.skipIf(_interpreters is None, "missing _interpreters module")
def test_static_type_concurrent_init_fini(self):
# gh-136421: When a managed static extension type is concurrently used
# by only subinterpreters, there was a crash due to the runtime state
# rather than an interpreter state being updated wrongly by mistaking
# the type's first initialization stage or last finalization one.
script = textwrap.dedent("""
import threading
import _interpreters

def run(id):
_interpreters.exec(id, "import _datetime; print('a', end='')")
_interpreters.destroy(id)

ids = [_interpreters.create() for i in range(10)]
ts = [threading.Thread(target=run, args=(id,)) for id in ids]
for t in ts:
t.start()
for t in ts:
t.join()
""")
_, out, err = script_helper.assert_python_ok('-c', script)
self.assertEqual(out, b'a' * 10)
self.assertEqual(err, b'')


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,2 @@
Fix crash in :mod:`datetime` when its static types are initialized or
finalized by multiple interpreters concurrently.
24 changes: 18 additions & 6 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7335,12 +7335,17 @@ init_static_types(PyInterpreterState *interp, int reloading)
if (reloading) {
return 0;
}

// `&...` is not a constant expression according to a strict reading
// of C standards. Fill tp_base at run-time rather than statically.
// See https://bugs.python.org/issue40777
PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType;
PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;
if (_Py_IsMainInterpreter(interp)) {
if (PyType_HasFeature(&PyDateTime_DateType, Py_TPFLAGS_READY)) {
// This function was already called from PyInit__datetime()
return 0;
}
// `&...` is not a constant expression according to a strict reading
// of C standards. Fill tp_base at run-time rather than statically.
// See https://bugs.python.org/issue40777
PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType;
PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;
}

/* Bases classes must be initialized before subclasses,
* so capi_types must have the types in the appropriate order. */
Expand Down Expand Up @@ -7564,6 +7569,13 @@ static PyModuleDef datetimemodule = {
PyMODINIT_FUNC
PyInit__datetime(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
// gh-136421: Ensure static types' runtime state gets cleared in shutting
// down the main interpreter rather than subinterpreters for concurrency.
assert(interp != NULL && _Py_IsMainInterpreter(interp));
if (init_static_types(interp, 0) < 0) {
return NULL;
}
return PyModuleDef_Init(&datetimemodule);
}

Expand Down
Loading