GeezerVison is showing github.com — `arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds · Issue #149006 · python/cpython
arraymodule, _decimal: possible lazy import caches reference leaks in free-threaded builds #149006
DescriptionBug description:
As pointed out by @htjworld in #148088 (comment):
I was wondering about the lazy init pattern in a free-threaded build. If two threads enter build_struct_time concurrently with st->time_struct_time == NULL, both would call PyImport_ImportModuleAttrString and one overwrite would leave a reference with an incremented refcount that never gets decref'd.
I noticed arraymodule.c and _decimal.c have the same pattern, so maybe this is just accepted. But _asynciomodule.c initializes eagerly in module_init() which avoids this structurally. Was lazy chosen deliberately here over eager init in init_state()?
While I've addressed this in #148088, I think that this is a valid concern, and import caches should use CAS, just like:
|
PyObject *func = FT_ATOMIC_LOAD_PTR(module_state->compile_template); |
|
if (func == NULL) { |
|
func = PyImport_ImportModuleAttrString("re", "_compile_template"); |
|
if (func == NULL) { |
|
return NULL; |
|
} |
|
#ifdef Py_GIL_DISABLED |
|
PyObject *other_func = NULL; |
|
if (!_Py_atomic_compare_exchange_ptr(&module_state->compile_template, &other_func, func)) { |
|
Py_DECREF(func); |
|
func = other_func; |
|
} |
|
#else |
|
Py_XSETREF(module_state->compile_template, func); |
|
#endif |
|
} |
If there's interest, I can do it.
(It seems similar to #149000, so perhaps a more thorough check warranted.)
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS Activitychanged the title [-]Possible lazy import caches reference leaks in free-threaded builds[/-] [+]`arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds[/+] on Apr 26, 2026htjworld commented on Apr 26, 2026 Hi @maurycy — interested in helping with this one. Happy to follow your lead since you opened it, but a couple of things I could pick up:
- Take a first pass at the CAS fix for
arraymodule.c or _decimal.c, whichever you're not already on
- Grep around for other sites with the same pattern and post a checklist here
Or if you'd rather drive both, happy to just review. Let me know what works. maurycy commented on Apr 26, 2026 ContributorAuthor More actions
Hi @maurycy — interested in helping with this one. Happy to follow your lead since you opened it, but a couple of things I could pick up:
- Take a first pass at the CAS fix for
arraymodule.c or _decimal.c, whichever you're not already on
- Grep around for other sites with the same pattern and post a checklist here
Or if you'd rather drive both, happy to just review. Let me know what works.
I'm not a core dev! Let's wait for a green light from the higher ups :-)
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|