gh-129107: make bytearray free-thread safe#129108
Conversation
ZeroIntensity
left a comment
There was a problem hiding this comment.
I'll yield to someone else, but this is one of the things that we probably shouldn't do with critical sections. bytearray is a hot object--we should keep performance in mind here. A lot (but not all!) of bytearray isn't prone to deadlocks because it doesn't typically re-enter.
I'm still undecided, but it might be worth borrowing from the FT list implementation for doing it locklessly.
|
I suggest to split this PR into 2:
|
Makes sense, iterator is smaller (and easier to backport?) so will break that out and send it separate up later today and leave this one as the main bytearray? |
|
Yup, SGTM, ping me for reviews. |
|
@colesbury This PR fixes thread safety of bytearray with critical section; do we want to have lock free reads for this too? |
No, probably not. At least not for now. |
|
Thank you very much for the continued efforts 🙇 Are there plans to backport this (and the follow-up PRs) to 3.13? |

Most of the work has been wrapping functions that need it in critical sections (one at a time, testing each one). There were some functions which were pointed directly at stringlib methods and needed critical sections, but since stringlib is shared with
strandbytesand those immutable types don't need locks, the stringlib functions were not touched. Instead, they were wrapped in functions which take object locks.There is work left to do, testing and doing the iterator. But starting as it is, this PR passes the test suite (at least on my system and build) as well as fixing the error cases presented in the script in the associated issue.
Functions which were looked at ("
-" means no critical section was needed, otherwise function was fixed in one way or another):Comments and guidance requested.