Skip to content

BUG: data races in _buffer_get_info when multiple threads try to access buffer of ndarray #30648

@kumaraditya303

Description

@kumaraditya303

Describe the issue:

Run the reproducer using a TSAN instrumented build of CPython and numpy.

Reproduce the code example:

import numpy as np
from threading import Thread, Barrier
import inspect
import random
arr = np.array([1, 2, 3, 4, 5])

flags = [inspect.BufferFlags.STRIDED, inspect.BufferFlags.READ]

barrier = Barrier(4)
def func():
    barrier.wait()
    arr.__buffer__(random.choice(flags))

threads = [Thread(target=func) for _ in range(4)]
for t in threads:
    t.start()
for t in threads:
    t.join()

Error message:

WARNING: ThreadSanitizer: data race (pid=6155)
  Read of size 8 at 0x000128060100 by thread T1:
    #0 _buffer_get_info <null> (_multiarray_umath.cpython-314t-darwin.so:arm64+0x37c1d8)
    #1 array_getbuffer <null> (_multiarray_umath.cpython-314t-darwin.so:arm64+0x37c5b8)
    #2 _PyMemoryView_FromBufferProc memoryobject.c:838 (libpython3.14t.dylib:arm64+0x122c1c)
    #3 buffer typeobject.c:9688 (libpython3.14t.dylib:arm64+0x1a48b0)
    #4 wrapperdescr_call descrobject.c:570 (libpython3.14t.dylib:arm64+0xa17ac)
    #5 _PyObject_MakeTpCall call.c:242 (libpython3.14t.dylib:arm64+0x8a3ac)
    #6 PyObject_Vectorcall call.c:327 (libpython3.14t.dylib:arm64+0x8af9c)
    #7 _PyEval_EvalFrameDefault generated_cases.c.h:1619 (libpython3.14t.dylib:arm64+0x27cc00)
    #8 _PyEval_Vector ceval.c:2083 (libpython3.14t.dylib:arm64+0x278928)
    #9 _PyFunction_Vectorcall call.c (libpython3.14t.dylib:arm64+0x8b54c)
    #10 method_vectorcall classobject.c:73 (libpython3.14t.dylib:arm64+0x8f9c0)
    #11 context_run context.c:728 (libpython3.14t.dylib:arm64+0x2c3d18)
    #12 method_vectorcall_FASTCALL_KEYWORDS descrobject.c:421 (libpython3.14t.dylib:arm64+0xa21cc)
    #13 PyObject_Vectorcall call.c:327 (libpython3.14t.dylib:arm64+0x8af00)
    #14 _PyEval_EvalFrameDefault generated_cases.c.h:1619 (libpython3.14t.dylib:arm64+0x27cc00)
    #15 _PyEval_Vector ceval.c:2083 (libpython3.14t.dylib:arm64+0x278928)
    #16 _PyFunction_Vectorcall call.c (libpython3.14t.dylib:arm64+0x8b54c)
    #17 method_vectorcall classobject.c:73 (libpython3.14t.dylib:arm64+0x8f9c0)
    #18 _PyObject_Call call.c:348 (libpython3.14t.dylib:arm64+0x8b1c0)
    #19 PyObject_Call call.c:373 (libpython3.14t.dylib:arm64+0x8b234)
    #20 thread_run _threadmodule.c:359 (libpython3.14t.dylib:arm64+0x419b3c)
    #21 pythread_wrapper thread_pthread.h:242 (libpython3.14t.dylib:arm64+0x369a20)

  Previous write of size 8 at 0x000128060100 by thread T4:
    #0 _buffer_info_new <null> (_multiarray_umath.cpython-314t-darwin.so:arm64+0x37dc84)
    #1 _buffer_get_info <null> (_multiarray_umath.cpython-314t-darwin.so:arm64+0x37c110)
    #2 array_getbuffer <null> (_multiarray_umath.cpython-314t-darwin.so:arm64+0x37c5b8)
    #3 _PyMemoryView_FromBufferProc memoryobject.c:838 (libpython3.14t.dylib:arm64+0x122c1c)
    #4 buffer typeobject.c:9688 (libpython3.14t.dylib:arm64+0x1a48b0)
    #5 wrapperdescr_call descrobject.c:570 (libpython3.14t.dylib:arm64+0xa17ac)
    #6 _PyObject_MakeTpCall call.c:242 (libpython3.14t.dylib:arm64+0x8a3ac)
    #7 PyObject_Vectorcall call.c:327 (libpython3.14t.dylib:arm64+0x8af9c)
    #8 _PyEval_EvalFrameDefault generated_cases.c.h:1619 (libpython3.14t.dylib:arm64+0x27cc00)
    #9 _PyEval_Vector ceval.c:2083 (libpython3.14t.dylib:arm64+0x278928)
    #10 _PyFunction_Vectorcall call.c (libpython3.14t.dylib:arm64+0x8b54c)
    #11 method_vectorcall classobject.c:73 (libpython3.14t.dylib:arm64+0x8f9c0)
    #12 context_run context.c:728 (libpython3.14t.dylib:arm64+0x2c3d18)
    #13 method_vectorcall_FASTCALL_KEYWORDS descrobject.c:421 (libpython3.14t.dylib:arm64+0xa21cc)
    #14 PyObject_Vectorcall call.c:327 (libpython3.14t.dylib:arm64+0x8af00)
    #15 _PyEval_EvalFrameDefault generated_cases.c.h:1619 (libpython3.14t.dylib:arm64+0x27cc00)
    #16 _PyEval_Vector ceval.c:2083 (libpython3.14t.dylib:arm64+0x278928)
    #17 _PyFunction_Vectorcall call.c (libpython3.14t.dylib:arm64+0x8b54c)
    #18 method_vectorcall classobject.c:73 (libpython3.14t.dylib:arm64+0x8f9c0)
    #19 _PyObject_Call call.c:348 (libpython3.14t.dylib:arm64+0x8b1c0)
    #20 PyObject_Call call.c:373 (libpython3.14t.dylib:arm64+0x8b234)
    #21 thread_run _threadmodule.c:359 (libpython3.14t.dylib:arm64+0x419b3c)
    #22 pythread_wrapper thread_pthread.h:242 (libpython3.14t.dylib:arm64+0x369a20)

  Thread T1 (tid=67013, running) created by main thread at:
    #0 pthread_create <null> (libclang_rt.tsan_osx_dynamic.dylib:arm64e+0x2f708)
    #1 do_start_joinable_thread thread_pthread.h:289 (libpython3.14t.dylib:arm64+0x3686e0)
    #2 PyThread_start_joinable_thread thread_pthread.h:331 (libpython3.14t.dylib:arm64+0x368528)
    #3 do_start_new_thread _threadmodule.c:1868 (libpython3.14t.dylib:arm64+0x4196f0)
    #4 thread_PyThread_start_joinable_thread _threadmodule.c:1991 (libpython3.14t.dylib:arm64+0x41847c)
    #5 cfunction_call methodobject.c:564 (libpython3.14t.dylib:arm64+0x12d450)
    #6 _PyObject_MakeTpCall call.c:242 (libpython3.14t.dylib:arm64+0x8a3ac)
    #7 PyObject_Vectorcall call.c:327 (libpython3.14t.dylib:arm64+0x8af9c)
    #8 _PyEval_EvalFrameDefault generated_cases.c.h:2959 (libpython3.14t.dylib:arm64+0x280794)
    #9 PyEval_EvalCode ceval.c:975 (libpython3.14t.dylib:arm64+0x278504)
    #10 run_mod pythonrun.c:1459 (libpython3.14t.dylib:arm64+0x347380)
    #11 _PyRun_SimpleFileObject pythonrun.c:521 (libpython3.14t.dylib:arm64+0x342a5c)
    #12 _PyRun_AnyFileObject pythonrun.c:81 (libpython3.14t.dylib:arm64+0x3421b0)
    #13 pymain_run_file main.c:429 (libpython3.14t.dylib:arm64+0x383bb0)
    #14 Py_RunMain main.c:775 (libpython3.14t.dylib:arm64+0x382fdc)
    #15 pymain_main main.c:805 (libpython3.14t.dylib:arm64+0x383448)
    #16 Py_BytesMain main.c:829 (libpython3.14t.dylib:arm64+0x38351c)
    #17 main python.c:15 (python3.14:arm64+0x100000714)

  Thread T4 (tid=67016, running) created by main thread at:
    #0 pthread_create <null> (libclang_rt.tsan_osx_dynamic.dylib:arm64e+0x2f708)
    #1 do_start_joinable_thread thread_pthread.h:289 (libpython3.14t.dylib:arm64+0x3686e0)
    #2 PyThread_start_joinable_thread thread_pthread.h:331 (libpython3.14t.dylib:arm64+0x368528)
    #3 do_start_new_thread _threadmodule.c:1868 (libpython3.14t.dylib:arm64+0x4196f0)
    #4 thread_PyThread_start_joinable_thread _threadmodule.c:1991 (libpython3.14t.dylib:arm64+0x41847c)
    #5 cfunction_call methodobject.c:564 (libpython3.14t.dylib:arm64+0x12d450)
    #6 _PyObject_MakeTpCall call.c:242 (libpython3.14t.dylib:arm64+0x8a3ac)
    #7 PyObject_Vectorcall call.c:327 (libpython3.14t.dylib:arm64+0x8af9c)
    #8 _PyEval_EvalFrameDefault generated_cases.c.h:3227 (libpython3.14t.dylib:arm64+0x28150c)
    #9 PyEval_EvalCode ceval.c:975 (libpython3.14t.dylib:arm64+0x278504)
    #10 run_mod pythonrun.c:1459 (libpython3.14t.dylib:arm64+0x347380)
    #11 _PyRun_SimpleFileObject pythonrun.c:521 (libpython3.14t.dylib:arm64+0x342a5c)
    #12 _PyRun_AnyFileObject pythonrun.c:81 (libpython3.14t.dylib:arm64+0x3421b0)
    #13 pymain_run_file main.c:429 (libpython3.14t.dylib:arm64+0x383bb0)
    #14 Py_RunMain main.c:775 (libpython3.14t.dylib:arm64+0x382fdc)
    #15 pymain_main main.c:805 (libpython3.14t.dylib:arm64+0x383448)
    #16 Py_BytesMain main.c:829 (libpython3.14t.dylib:arm64+0x38351c)
    #17 main python.c:15 (python3.14:arm64+0x100000714)

SUMMARY: ThreadSanitizer: data race (_multiarray_umath.cpython-314t-darwin.so:arm64+0x37c1d8) in _buffer_get_info+0x154

Python and NumPy Versions:

2.5.0.dev0+git20260114.a90ef57
3.14.2+ free-threading build (heads/3.14:c3fbe28, Dec 12 2025, 18:50:21) [Clang 17.0.0 (clang-1700.4.4.1)]

Runtime Environment:

[{'numpy_version': '2.5.0.dev0+git20260114.a90ef57',
'python': '3.14.2+ free-threading build (heads/3.14:c3fbe28, Dec 12 2025, '
'18:50:21) [Clang 17.0.0 (clang-1700.4.4.1)]',
'uname': uname_result(system='Darwin', node='Kumars-MacBook-Air.local', release='25.2.0', version='Darwin Kernel Version 25.2.0: Tue Nov 18 21:08:48 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T8132', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP', 'ASIMDDP'],
'not_found': ['ASIMDFHM', 'SVE']}},
{'ignore_floating_point_errors_in_matmul': True}]

How does this issue affect you or how did you find it:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - Bug39 - free-threadingPRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions