From f1e72253e4ed506e365a7ba494cba385f2a351c5 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 8 Jul 2025 22:58:20 +0900 Subject: [PATCH 1/5] Fix crash when _datetime is been initialized in multiple sub-interpreters --- .../2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst | 2 ++ Modules/_datetimemodule.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst new file mode 100644 index 00000000000000..667f51bbed47a1 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst @@ -0,0 +1,2 @@ +Fix a crash when :mod:`_datetime` is being initialized in multiple +sub-interpreters at the same time. diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 7a6426593d021f..7fd4a46d5a9891 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -7344,11 +7344,16 @@ init_static_types(PyInterpreterState *interp, int reloading) /* Bases classes must be initialized before subclasses, * so capi_types must have the types in the appropriate order. */ + static PyMutex mutex = {0}; for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) { PyTypeObject *type = capi_types[i]; - if (_PyStaticType_InitForExtension(interp, type) < 0) { + PyMutex_Lock(&mutex); + int ret = _PyStaticType_InitForExtension(interp, type); + if (ret < 0) { + PyMutex_Unlock(&mutex); return -1; } + PyMutex_Unlock(&mutex); } return 0; From b6735f6196bb37bcc6dd59a03ae0b4cdf4a438c7 Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 8 Jul 2025 23:15:49 +0900 Subject: [PATCH 2/5] Fix RST markers --- .../2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst index 667f51bbed47a1..af6163b594f803 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst @@ -1,2 +1,2 @@ -Fix a crash when :mod:`_datetime` is being initialized in multiple +Fix a crash when ``_datetime`` is being imported in multiple sub-interpreters at the same time. From 67cf8fd7d8ac03dfeb2fc8a235082b6452e53894 Mon Sep 17 00:00:00 2001 From: AN Long Date: Wed, 9 Jul 2025 01:52:13 +0900 Subject: [PATCH 3/5] Simplify the codes --- Modules/_datetimemodule.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 7fd4a46d5a9891..4105f5d651c89e 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -7349,11 +7349,10 @@ init_static_types(PyInterpreterState *interp, int reloading) PyTypeObject *type = capi_types[i]; PyMutex_Lock(&mutex); int ret = _PyStaticType_InitForExtension(interp, type); + PyMutex_Unlock(&mutex); if (ret < 0) { - PyMutex_Unlock(&mutex); return -1; } - PyMutex_Unlock(&mutex); } return 0; From 1637eb91128f275cda81987d5ab7e1558b02c84e Mon Sep 17 00:00:00 2001 From: AN Long Date: Sat, 12 Jul 2025 20:53:05 +0900 Subject: [PATCH 4/5] Increase mutex protect region --- Modules/_datetimemodule.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 4105f5d651c89e..90bad2de5ba4da 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -7336,6 +7336,8 @@ init_static_types(PyInterpreterState *interp, int reloading) return 0; } + static PyMutex mutex = {0}; + PyMutex_Lock(&mutex); // `&...` 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 @@ -7344,17 +7346,16 @@ init_static_types(PyInterpreterState *interp, int reloading) /* Bases classes must be initialized before subclasses, * so capi_types must have the types in the appropriate order. */ - static PyMutex mutex = {0}; for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) { PyTypeObject *type = capi_types[i]; - PyMutex_Lock(&mutex); int ret = _PyStaticType_InitForExtension(interp, type); - PyMutex_Unlock(&mutex); if (ret < 0) { + PyMutex_Unlock(&mutex); return -1; } } + PyMutex_Unlock(&mutex); return 0; } From d1b8728d24aea1983b7be7b5cbf9cb6604719cc1 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sat, 12 Jul 2025 22:05:14 +0900 Subject: [PATCH 5/5] Update Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst Co-authored-by: Peter Bierma --- .../2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst index af6163b594f803..ad2e094fcbfc83 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-00-58.gh-issue-136421.uzieFA.rst @@ -1,2 +1,2 @@ -Fix a crash when ``_datetime`` is being imported in multiple +Fix a crash when :mod:`datetime` is being imported in multiple sub-interpreters at the same time.