python / python/cpython

It isn't possible to temporarily resurrect objects safely in the Py3.15t limited API

Aperta
#152,406 8 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core topic-C-API type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

In Cython we do something like

Py_SET_REFCNT(o, Py_REFCNT(o) + 1);
// Run user implemented deallocation code
Py_SET_REFCNT(o, Py_REFCNT(o) - 1);

The reason for this is that we don't want any refcounting (e.g. of temps) within the user code to accidentally trigger a recursive call to dealloc (the user code really shouldn't resurrect the object and it's their own problem if they do, but it is reasonable for it to generate temporaries, which may well be out of their control anyway).

This turns out not to be thread-safe in the freethreaded build because it makes TryIncRef succeed. Outside the limited API it's possible to fix by manual access to the reference count, but obviously not inside the limited API.

It would be nice to have a way of to avoid this.

  • Most convenient would be to properly access _PyObject_ResurrectStart and _PyObject_ResurrectEnd.
  • The other thing that could work is some way to defer the deallocation until the next GC cycle (when I think any TryIncRefs would have fall out of scope anyway). I suspect I could manually arrange to do that, although it feels fragile, possibly relies on the GC being single-threaded, and which I haven't tested (so may not work...)
CPython versions tested on:

3.15

Operating systems tested on:

No response

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 esaminando la superficie della limited-API coinvolta nella resurrezione del conteggio dei riferimenti e nel comportamento di TryIncRef in modalità free-threaded descritto qui, quindi confrontala con _PyObject_ResurrectStart e _PyObject_ResurrectEnd e con la issue #7769 di Cython. Il lavoro è completo quando esiste un meccanismo definito e thread-safe della limited-API che eviti una resurrezione temporanea non sicura durante il codice di deallocazione dell’utente.

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

Valutazione

Stack tecnologico
c, python
Ambito
api
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.