python / python/cpython

Data race on the GC debug flag (gc.set_debug/get_debug) in free-threading builds

Aperta
#153,014 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug report

In a free-threading build (--disable-gil), the garbage collector debug flag
gcstate->debug is read and written without synchronisation:

  • Write: gc_set_debug_impl() (Modules/gcmodule.c) does a plain
    gcstate->debug = flags. Unlike gc_set_threshold_impl(), which runs its
    free-threading branch under _PyEval_StopTheWorld(), gc.set_debug() takes
    no lock and does not stop the world.
  • Read: gc_get_debug_impl() returns gcstate->debug directly, and the
    collector in Python/gc_free_threading.c reads gcstate->debug /
    interp->gc.debug in several places while walking the graph.

So one thread calling gc.set_debug() concurrently with another calling
gc.get_debug() or triggering a collection is an unsynchronised read/write of
the same int. The flag is only an int, so the effect stays benign at the
Python level, but it is undefined behaviour under C11 and ThreadSanitizer
reports it as a data race.

This is the same class of issue already fixed for sys dlopenflags
(gh-151644) and gc.get_stats() (gh-151646). gc.enable() / gc.disable()
in the same file already access gcstate->enabled atomically; the debug flag
was missed.

ThreadSanitizer output

Built with ./configure --with-thread-sanitizer --disable-gil and stressed
with concurrent gc.set_debug() / gc.get_debug() plus a thread churning
cyclic garbage so the collector runs:

WARNING: ThreadSanitizer: data race
  Write of size 4 at 0x...6c by thread T2:
    #0 gc_set_debug gcmodule.c.h:186
  Previous write of size 4 at 0x...6c by thread T1:
    #0 gc_set_debug gcmodule.c.h:186
  Location is global '_PyRuntime'

How to reproduce

  1. ./configure --with-thread-sanitizer --disable-gil && make
  2. Run a script that starts a few threads calling gc.set_debug(...) /
    gc.get_debug() in a loop, plus a thread that builds reference cycles and
    calls gc.collect().
  3. TSan reports the write/write race on gcstate->debug.

Suggested fix

Access the flag with FT_ATOMIC_STORE_INT_RELAXED / FT_ATOMIC_LOAD_INT_RELAXED
in gc_set_debug_impl() / gc_get_debug_impl() and in the collector reads,
matching how gcstate->enabled and dlopenflags are already handled. Relaxed
ordering is correct for an independent int flag. These wrappers compile to a
plain load/store in the default (GIL) build, so there is no change there.

Linked PRs
  • gh-153015

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

Esamina prima la PR collegata gh-153015, poi ispeziona gc_set_debug_impl() e gc_get_debug_impl() in Modules/gcmodule.c e le letture di gcstate-debug in Python/gc_free_threading.c. Ricompila con --with-thread-sanitizer --disable-gil ed esegui il riproduttore concorrente di gc.set_debug(), gc.get_debug() e della raccolta; il lavoro è completato quando la race segnalata non è più presente.

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

Valutazione

Stack tecnologico
c, python
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.