python / python/cpython

Data race between `sys._current_exceptions()` and a concurrently attaching thread

Aperta
#152,741 2 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
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug description:

In the free-threaded build, sys._current_exceptions() iterates every thread state under stop-the-world + HEAD_LOCK and reads t->thread_id.

https://github.com/python/cpython/blob/ecdef1773006529b0fea6639d0effeecbb41679c/Python/pystate.c#L2808-L2817

But a newly started thread sets tstate->thread_id in bind_tstate() without holding HEAD_LOCK. Its PyThreadState is already on the interpreter's thread list by this point.

https://github.com/python/cpython/blob/ecdef1773006529b0fea6639d0effeecbb41679c/Python/pystate.c#L164-L178

A thread that is still attaching is not yet stop-the-world-stoppable. _PyEval_StopTheWorldAll() therefore does not pause it, and its unlocked write to tstate->thread_id races the stop-the-world reader in _PyThread_CurrentExceptions.

Reproducer:

import sys
import threading

stop = threading.Event()

def reader():                      # keep stop-the-world almost always active
    for _ in range(20000):
        sys._current_exceptions()

def churn():                       # continuously attach new threads (bind_tstate)
    for _ in range(20000):
        t = threading.Thread(target=lambda: None)
        t.start()
        t.join()

readers  = [threading.Thread(target=reader) for _ in range(8)]
churners = [threading.Thread(target=churn)  for _ in range(4)]
for t in readers + churners: t.start()
for t in readers + churners: t.join()

TSAN Report:

WARNING: ThreadSanitizer: data race (pid=94157)
  Read of size 8 at 0x0001081180b0 by thread T1:
    #0 _PyThread_CurrentExceptions pystate.c:2817
    #1 sys__current_exceptions sysmodule.c.h:1180
    #2 cfunction_vectorcall_NOARGS methodobject.c:508
    #3 PyObject_Vectorcall call.c:327
    #4 _Py_VectorCallInstrumentation_StackRefSteal ceval.c:768
    #5 _PyEval_EvalFrameDefault generated_cases.c.h:1906
...
  Previous write of size 8 by thread T9:
    #0 bind_tstate pystate.c:178
    #1 _PyThreadState_Bind pystate.c:2660
    #2 thread_run _threadmodule.c:384
    #3 pythread_wrapper thread_pthread.h:234

SUMMARY: ThreadSanitizer: data race pystate.c:2817 in _PyThread_CurrentExceptions
CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-152755
  • gh-156202

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 con bind_tstate() e _PyThread_CurrentExceptions in Python/pystate.c, quindi esamina le PR collegate gh-152755 e gh-156202. Esegui il riproduttore free-threaded fornito con ThreadSanitizer e verifica che la race condition segnalata tra la scrittura e la lettura di thread_id sia risolta senza introdurre nuovi errori.

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

Valutazione

Stack tecnologico
python
Ambito
operating-systems
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 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.