-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
ctypes pointer writes are not thread safe #128182
Copy link
Copy link
Closed
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Metadata
Metadata
Assignees
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtopic-ctypestopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
Bug description:
Part of #127945.
ctypesC data objects have an internal pointer for what they're looking at (b_ptr). This field itself is generally fine to read non-atomically, because ctypes objects don't tend to overwrite the pointer that they're pointing to, but reading and writing the pointer's contents (such as viamemcpy) isn't thread safe. This can be seen primarily with arrays:There's really only two options here, because the lockless
_Py_atomicAPIs can't be used for allocations of arbitrary sizes: add a critical section around all functions touchingb_ptr, or just add aPyMutexaround it.I think that a mutex is the right way to go here:
memcpy.__call__), so they can't get wrapped with@critical_section. We'd need ugly wrapper functions.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
ctypes#128490_ctypes.PyCDatagetters and setters #132082ctypesto solely critical sections #132133