python / python/cpython

nogil inconsistent empty list while sorting

未關閉
#126,559 6 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.13 3.14 interpreter-core topic-free-threading type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先在 free-threading CPython 建置上執行提供的重現程式,接著閱讀 PEP 703 中關於容器執行緒安全性的章節,以及 issue #126136 中的討論。在判定是否需要修正與回歸測試之前,必須先確立並行進行 list 排序、取得長度和字串轉換時的預期行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。