python / python/cpython

nogil inconsistent empty list while sorting

Aperta
#126,559 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.13 3.14 interpreter-core 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 report

Bug description:

Hi,

We're a research group focused on testing concurrent runtimes. Our work-in-progress prototype found a violation of atomicity on the current nogil build when using concurrent operations on the same list. The program below shows the wrong behavior.

from threading import Thread,Barrier,Lock

def t1(b1,l,r):
    b1.wait()
    r[0] = l.__len__()

def t2(b1,l,r):
    b1.wait()
    r[0] = l.__str__()

def t3(b1,l):
    b1.wait()
    l.sort()

def normalSetTest(i):

    sharedList = [1,2,3] * 100
    barrier = Barrier(2)
    res = [ None ]
    threads = [
            Thread(target= t1, args=(barrier,sharedList,res,)),
            #Thread(target= t2, args=(barrier,sharedList,res,)),
            Thread(target= t3, args=(barrier,sharedList,)),
            ]


    for t in threads:
        t.start()
    for t in threads:
        t.join()

    if res[0] in [ 0, '[]' ]:
        print("\tfound bug: " + str(res[0]))

print("test begin...")

for n in range(0,10):
    threads = []

    for i in range(0,1):
        threads.append(Thread(target= normalSetTest, args=(n,)))

    for t in threads:
        t.start()

    for t in threads:
        t.join()

print("test Done")

A list with 300 integers is sorted in t3 while another thread either gets the size of the list t1 or turns the list into a string t2. Running the code above will show threads t1 and t2 finding an inconsistent empty list with length 0 or turned into the string "[]".
Our tool did not find any other interesting values: either it's an expected value (original list or sorted list) OR it's empty.

Sample output:

test begin...
        found bug: 0
        found bug: 0
        found bug: 0
        found bug: 0
        found bug: 0
test Done

We're happy to provide more details about this bug, and to help developers reproducing it.

Output of python -VV: Python 3.14.0a1+ experimental free-threading build (heads/main:faa3272fb8d, Oct 29 2024, 09:14:25) [GCC 14.2.1 20240805]

@flypoodles and @overlorde are part of the team, adding them so they get notified about further discussion.

I believe this issue is part of the ongoing conversation on #126136 about acceptable behaviors of containers operated by many threads concurrently, described in the original nogil PEP: https://peps.python.org/pep-0703/#container-thread-safety

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

Linux

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 eseguendo il riproduttore fornito su una build di CPython free-threading, quindi leggi la sezione di PEP 703 sulla thread-safety dei container e la discussione in issue #126136. Il comportamento previsto per l'ordinamento concorrente delle liste, la determinazione della lunghezza e la conversione in stringa deve essere stabilito prima di determinare se sono necessarie una correzione e un test di regressione.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.