python / python/cpython

`arraymodule`, `_decimal`: possible lazy import caches reference leaks in free-threaded builds

Open
#149,006 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic-free-threading type-bug
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_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:

https://github.com/python/cpython/blob/7e0a0be4097f9d29d66fe23f5af86f18a34ed7dd/Modules/_sre/sre.c#L1173-L1188

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.