`arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug description:
As pointed out by @htjworld in https://github.com/python/cpython/pull/148088#issuecomment-4196296604:
I was wondering about the lazy init pattern in a free-threaded build. If two threads enter
build_struct_timeconcurrently withst->time_struct_time == NULL, both would callPyImport_ImportModuleAttrStringand one overwrite would leave a reference with an incremented refcount that never gets decref'd.
I noticed
arraymodule.cand_decimal.chave 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:
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the lazy initialization in arraymodule.c and _decimal.c with the eager initialization in _asynciomodule.c and the CAS pattern in Modules/_sre/sre.c. Check the free-threaded paths for concurrent initialization and reference leaks; done means the affected import caches are safe under that concurrency pattern and the relevant behavior is covered by validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100