ctypes
pointer writes are not thread safe
#128182
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
Bug report
Bug description:
Part of #127945.
ctypes
C 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_atomic
APIs can't be used for allocations of arbitrary sizes: add a critical section around all functions touchingb_ptr
, or just add aPyMutex
around 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
The text was updated successfully, but these errors were encountered: