-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Data races in free-threaded python on Py_buffer use #130977
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
Comments
ping @colesbury |
The buffer protocol is really to give external C-like code fast access to C array data. At least in C code it was always possible to work on a buffer with the GIL released. So it was never something where Python enforced the thread safety. I can't imagine that accessing it through memoryview and Python can really be made usefully thread-safe either because it doesn't know anything about what assumptions other users of the buffer are making. There has been some talk of adding Rust-style borrow-checked buffers (which would have many readers and 0 writers, or 0 readers and 1 writer) which would be safe, but that would be new feature. |
Not thread-safe, but free from data races, which I understand to be a goal of free-threaded python. Would have to make all RW-buffer accesses atomic? |
Fair enough - it sounds like the main value in this would be keeping address sanitizer quiet more than anything else. But there may be a genuine value in that. |
Alright, but if going this route, some questions: I am not aware of an atomic way to do If giving all buffer accesses this treatment (and other things) then this will be common functionality. Should be python-wide accessible? |
You can't do atomic memcpy. Even if you lock the buffer pointer, you can't stop other thread from writing to memory using this pointer (sorry if this obvious). |
Yeah, sorry, I wasn't being clear, see the EDIT above - "atomic" in terms of not triggering TSAN, not that the entire block copy will be atomic - so copying memory using relaxed atomic load/store ops. |
I'm afraid it will be very slow in this case. |
It has already been explained to me that not all data races are to be eliminated so this discussion is a bit moot, but to clear up the remaining points:
In any case, like I said for now not all data races are to be squashed so no "atomic" memcpy. |
Could you please share a link to the timings? I'm not sure I saw it. |
This is in the context of another PR and rough timings but here: #130771 (comment) Also note what the "atomic" memcpy compiles to further down, a cache line aligned block of 8 byte wide load/store instructions. |
Thanks! |
Bug report
Bug description:
memoryview
is used here for demonstration purposes, this should pop up in anything that uses the buffer interface.Reproducer:
Output:
Is this an issue?
CPython versions tested on:
3.14
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: