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 stgdict PyCStructUnionType_update_stginfo under free-threading #128570

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

Race in stgdict PyCStructUnionType_update_stginfo under free-threading #128570

vfdev-5 opened this issue Jan 7, 2025 · 0 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 make_nd_memref_descriptor(rank, dtype):
    class MemRefDescriptor(ctypes.Structure):
        _fields_ = [
            ("aligned", ctypes.POINTER(dtype)),
        ]

    return MemRefDescriptor


class F32(ctypes.Structure):
    _fields_ = [("f32", ctypes.c_float)]


def test_ctypes():
    arg1_ndim = 1
    ctp = F32
    ctypes.pointer(
        ctypes.pointer(make_nd_memref_descriptor(arg1_ndim, ctp)())
    )


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=264065)
  Read of size 4 at 0x7fffb80a0430 by thread T17:
    #0 PyCStructUnionType_update_stginfo /project/cpython/./Modules/_ctypes/stgdict.c:438:19 (_ctypes.cpython-313t-x86_64-linux-gnu.so+0x20976) (BuildId: 6a7727f376cee83a3124898fe6eb84e45e3a54bc)
    #1 PyCStructType_setattro /project/cpython/./Modules/_ctypes/_ctypes.c:1096:16 (_ctypes.cpython-313t-x86_64-linux-gnu.so+0xc809) (BuildId: 6a7727f376cee83a3124898fe6eb84e45e3a54bc)
    #2 PyObject_SetAttr /project/cpython/Objects/object.c:1415:15 (python3.13t+0x294fba) (BuildId: 65e52348ca9319985f9f5e265fd43aab2c867d59)
    #3 StructUnionType_init /project/cpython/./Modules/_ctypes/_ctypes.c:685:13 (_ctypes.cpython-313t-x86_64-linux-gnu.so+0xe222) (BuildId: 6a7727f376cee83a3124898fe6eb84e45e3a54bc)
    #4 PyCStructType_init /project/cpython/./Modules/_ctypes/_ctypes.c:715:12 (_ctypes.cpython-313t-x86_64-linux-gnu.so+0xc84a) (BuildId: 6a7727f376cee83a3124898fe6eb84e45e3a54bc)

Full traceback

Related to #127945

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
@picnixz picnixz added topic-ctypes extension-modules C modules in the Modules dir labels Jan 7, 2025
@ZeroIntensity ZeroIntensity added 3.13 bugs and security fixes topic-free-threading 3.14 new features, bugs and security fixes labels Jan 7, 2025
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

3 participants