gh-112087: Make list_repr and list_length to be thread-safe by corona10 · Pull Request #114582 · python/cpython
@serhiy-storchaka wrote:
I think that either all reads and writes of ob_size should be atomic, or they all should be in critical sections.
I think we should follow slightly different pattern in list:
- Writes to
ob_sizeshould be atomic AND in critical sections - Reads from
ob_sizeshould be either atomic OR in critical sections
Exceptions: initialization, deallocation, and certain special functions can use plain accesses without critical sections (e.g., PyList_New(), list_dealloc, list_traverse).
The motivation behind this is that we will have some accesses outside of critical sections (see "Optimistic dict and list Access Summary" in PEP 703), so we can't use critical sections everywhere, but we will use them for most operations.
At the same time, I think we should prefer plain, non-atomic reads from within critical sections. We could conservatively use atomic reads and still be correct, but my experience is that adding atomic operations where they are not necessary tends to hide data races from thread sanitizer.
Note that writes to ob_size have to be at atomic even if they are within critical sections because there might be concurrent reads.