Data race on the GC debug flag (gc.set_debug/get_debug) in free-threading builds
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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. Unlikegc_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()returnsgcstate->debugdirectly, and the
collector inPython/gc_free_threading.creadsgcstate->debug/
interp->gc.debugin 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
./configure --with-thread-sanitizer --disable-gil && make- 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
callsgc.collect(). - 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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Überprüfe zuerst den verknüpften PR gh-153015, untersuche anschließend gc_set_debug_impl() und gc_get_debug_impl() in Modules/gcmodule.c sowie die gcstate-debug-Lesezugriffe in Python/gc_free_threading.c. Baue mit --with-thread-sanitizer --disable-gil neu und führe den Reproducer für gleichzeitige Aufrufe von gc.set_debug(), gc.get_debug() und der Garbage Collection aus; abgeschlossen ist die Aufgabe, wenn der gemeldete Race nicht mehr auftritt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- c, python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 25/100