Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race in ctypes PyCFuncPtr_new under free-threading #128567

Open
Tracked by #127945
vfdev-5 opened this issue Jan 7, 2025 · 2 comments
Open
Tracked by #127945

Race in ctypes PyCFuncPtr_new under free-threading #128567

vfdev-5 opened this issue Jan 7, 2025 · 2 comments
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir topic-ctypes topic-free-threading type-bug An unexpected behavior, bug, or error

Comments

@vfdev-5
Copy link

vfdev-5 commented Jan 7, 2025

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

@vfdev-5 vfdev-5 added the type-bug An unexpected behavior, bug, or error label Jan 7, 2025
@ZeroIntensity ZeroIntensity added topic-ctypes extension-modules C modules in the Modules dir 3.13 bugs and security fixes topic-free-threading 3.14 new features, bugs and security fixes labels Jan 7, 2025
@ZeroIntensity
Copy link
Member

At a glance, this one looks like a race in libffi. TSan is showing races in ffi_closure_alloc on my end.

@vfdev-5
Copy link
Author

vfdev-5 commented Jan 7, 2025

I had multiple TSAN outputs: sometimes with ffi_closure_alloc as you see and also the above warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir topic-ctypes topic-free-threading type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants