-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
ctypes
pointer writes are not thread safe
#128182
Comments
Struct packing suggestion: change |
The merged PR uses critical sections. |
Thanks for merging! I'm going to close this as completed, because otherwise it will just sit here and collect dust for a while until we go for mutexes. I'll open a seperate issue once we're ready to do that. |
OK. Feel free to ping me for a progress report, especially if you don't hear from me in ~a month :) |
I am not sure if it is worth the effort to change it to mutex instead of critical section. It is easy to miss re-entrancy with a mutex and concurrent reading and writing to a pointer isn't an important use case. I propose to drop moving a mutex and instead use critical sections on functions with AC |
I'm inclined to argue otherwise. ctypes is broad and used for all sorts of things, so I'm not sure we should immediately rule out concurrency as a use-case. ctypes is used for shared memory between processes, right? cc @colesbury though. |
I don't think we should be synchronizing accesses to ctypes data structures. It's not broad enough to be safe: pointers can overlap. And ctypes is a low level API that can already crash if it's misused.
I'm not aware of people using ctypes for that. I guess you can do that if you have shared memory that's also writable between processes. But any synchronization with |
Yeah, but I'd rather do what we can to be a little helpful. Spurious crashes caused by races with ctypes objects will definitely not be fun to debug. Kumar's point was more that we shouldn't optimize for using ctypes objects concurrently (i.e, using |
yes, it is much easier to make functions like |
@ZeroIntensity There a few more issues I found in
Both of these should be called while holding the critical section as currently tsan reports them with data races when ctypes tests are run in parallel. |
It seems to me that |
#132133. For maintenance/simplicity reasons, it might be worth entirely removing the inline critical sections and using |
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
Linked PRs
ctypes
#128490_ctypes.PyCData
getters and setters #132082ctypes
to solely critical sections #132133The text was updated successfully, but these errors were encountered: