Skip to content

Data races in free-threaded python on Py_buffer use #130977

Closed
@tom-pytel

Description

@tom-pytel

Bug report

Bug description:

memoryview is used here for demonstration purposes, this should pop up in anything that uses the buffer interface.

Reproducer:

import threading


def copy_back_and_forth(b, a, d, count):
    b.wait()
    for _ in range(count):
        a[0] = d[1]
        a[1] = d[0]


def check(funcs, *args):
    barrier = threading.Barrier(len(funcs))
    thrds = []

    for func in funcs:
        thrd = threading.Thread(target=func, args=(barrier, *args))

        thrds.append(thrd)
        thrd.start()

    for thrd in thrds:
        thrd.join()


if __name__ == '__main__':
    b = bytearray([0, 1])
    while True:
        check([copy_back_and_forth] * 10, memoryview(b), memoryview(b), 100)

Output:

WARNING: ThreadSanitizer: data race (pid=25332)
  Write of size 1 at 0x7f618e090d91 by thread T20:
    #0 pack_single Objects/memoryobject.c:1926 (python+0x24ed75)
    #1 memory_ass_sub Objects/memoryobject.c:2688 (python+0x25015a)
    #2 PyObject_SetItem Objects/abstract.c:232 (python+0x150cd6)
    #3 _PyEval_EvalFrameDefault Python/generated_cases.c.h:11143 (python+0x466235)
    #4 _PyEval_EvalFrame Include/internal/pycore_ceval.h:116 (python+0x46db5c)
    #5 _PyEval_Vector Python/ceval.c:1820 (python+0x46db5c)
    #6 _PyFunction_Vectorcall Objects/call.c:413 (python+0x187eb4)
    #7 _PyObject_VectorcallTstate Include/internal/pycore_call.h:167 (python+0x18fb2c)
    #8 method_vectorcall Objects/classobject.c:72 (python+0x18fb2c)
    #9 _PyVectorcall_Call Objects/call.c:273 (python+0x18b9a7)
    #10 _PyObject_Call Objects/call.c:348 (python+0x18be9f)
    #11 PyObject_Call Objects/call.c:373 (python+0x18bf24)
    #12 thread_run Modules/_threadmodule.c:354 (python+0x668b93)
    #13 pythread_wrapper Python/thread_pthread.h:242 (python+0x57d1ea)

  Previous read of size 1 at 0x7f618e090d91 by thread T11:
    #0 unpack_single Objects/memoryobject.c:1803 (python+0x24b841)
    #1 memory_item Objects/memoryobject.c:2471 (python+0x24c79a)
    #2 memory_subscript Objects/memoryobject.c:2607 (python+0x254d56)
    #3 PyObject_GetItem Objects/abstract.c:158 (python+0x1504c1)
    #4 _PyEval_EvalFrameDefault Python/generated_cases.c.h:62 (python+0x41b75c)
    #5 _PyEval_EvalFrame Include/internal/pycore_ceval.h:116 (python+0x46db5c)
    #6 _PyEval_Vector Python/ceval.c:1820 (python+0x46db5c)
    #7 _PyFunction_Vectorcall Objects/call.c:413 (python+0x187eb4)
    #8 _PyObject_VectorcallTstate Include/internal/pycore_call.h:167 (python+0x18fb2c)
    #9 method_vectorcall Objects/classobject.c:72 (python+0x18fb2c)
    #10 _PyVectorcall_Call Objects/call.c:273 (python+0x18b9a7)
    #11 _PyObject_Call Objects/call.c:348 (python+0x18be9f)
    #12 PyObject_Call Objects/call.c:373 (python+0x18bf24)
    #13 thread_run Modules/_threadmodule.c:354 (python+0x668b93)
    #14 pythread_wrapper Python/thread_pthread.h:242 (python+0x57d1ea)

Is this an issue?

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions