python / python/cpython

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

Aperta
#149,006 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic-free-threading type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia confrontando l’inizializzazione lazy in arraymodule.c e _decimal.c con l’inizializzazione eager in _asynciomodule.c e il pattern CAS in Modules/_sre/sre.c. Controlla i percorsi free-threaded per verificare la presenza di inizializzazione concorrente e perdite di riferimenti; il lavoro è completo quando le cache di importazione interessate sono sicure con questo pattern di concorrenza e il comportamento rilevante è coperto dalla validazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, python
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.