Skip to content

Race in ctypes PyCFuncPtr_new under free-threading  #128567

@vfdev-5

Description

@vfdev-5

Bug report

Bug description:

I built cpython (3.13 branch) with free-threading and TSAN. The following python code reports TSAN warnings:

import ctypes
import concurrent.futures
import threading


def raw_func(x):
    pass


def test_ctypes():
    def lookup():
        prototype = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
        return prototype(raw_func)

    ctypes_args = ()
    func = lookup()
    packed_args = (ctypes.c_void_p * len(ctypes_args))()
    for argNum in range(len(ctypes_args)):
        packed_args[argNum] = ctypes.cast(ctypes_args[argNum], ctypes.c_void_p)
    func(packed_args)


if __name__ == "__main__":
    num_workers = 20

    barrier = threading.Barrier(num_workers)

    def closure():
        barrier.wait()
        test_ctypes()

    with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
        futures = []
        for i in range(num_workers):
            futures.append(executor.submit(closure))
        assert len(list(f.result() for f in futures)) == num_workers

TSAN report extract:

WARNING: ThreadSanitizer: data race (pid=261974)
  Write of size 4 at 0x7fffbe0d0430 by thread T5:
    #0 generic_pycdata_new /project/cpython/./Modules/_ctypes/_ctypes.c:3387:17 (_ctypes.cpython-313t-x86_64-linux-gnu.so+0xdbb7) (BuildId: 6a7727f376cee83a3124898fe6eb84e45e3a54bc)
    #1 PyCFuncPtr_new /project/cpython/./Modules/_ctypes/_ctypes.c:3963:32 (_ctypes.cpython-313t-x86_64-linux-gnu.so+0x18bbb) (BuildId: 6a7727f376cee83a3124898fe6eb84e45e3a54bc)
    #2 type_call /project/cpython/Objects/typeobject.c:1981:11 (python3.13t+0x2eb032) (BuildId: 65e52348ca9319985f9f5e265fd43aab2c867d59)
    #3 _PyObject_MakeTpCall /project/cpython/Objects/call.c:242:18 (python3.13t+0x1ea44c) (BuildId: 65e52348ca9319985f9f5e265fd43aab2c867d59)
    #4 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:166:16 (python3.13t+0x1eb0a8) (BuildId: 65e52348ca9319985f9f5e265fd43aab2c867d59)
    #5 PyObject_Vectorcall /project/cpython/Objects/call.c:327:12 (python3.13t+0x1eb0a8)

Full traceback

Related #128182

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions