Skip to content

gh-115999: Make list and tuple iteration more thread-safe.#128637

Merged
Yhg1s merged 17 commits intopython:mainfrom
Yhg1s:list-realloc
Feb 19, 2025
Merged

gh-115999: Make list and tuple iteration more thread-safe.#128637
Yhg1s merged 17 commits intopython:mainfrom
Yhg1s:list-realloc

Conversation

@Yhg1s
Copy link
Member

@Yhg1s Yhg1s commented Jan 8, 2025

Make tuple iteration more thread-safe, and actually test concurrent iteration of tuple, range and list. (This is prep work for enabling specialization of FOR_ITER in free-threaded builds.) The basic premise is:

  • Iterating over a shared iterable (list, tuple or range) should be safe, not involve data races, and behave like iteration normally does.

  • Using a shared iterator should not crash or involve data races, and should only produce items regular iteration would produce. It is not guaranteed to produce all items, or produce each item only once. (This is not the case for range iteration even after this PR.)

Providing stronger guarantees is possible for some of these iterators, but it's not always straight-forward and can significantly hamper the common case. Since iterators in general aren't shared between threads, and it's simply impossible to concurrently use many iterators (like generators), better to make sharing iterators without explicit synchronization clearly wrong.

Specific issues fixed in order to make the tests pass:

  • List iteration could occasionally fail an assertion when a shared list was shrunk and an item past the new end was retrieved concurrently. There's still some unsafety when deleting/inserting multiple items through for example slice assignment, which uses memmove/memcpy.

  • Tuple iteration could occasionally crash when the iterator's reference to the tuple was cleared on exhaustion. Like with list iteration, in free-threaded builds we can't safely and efficiently clear the iterator's reference to the iterable (doing it safely would mean extra, slow refcount operations), so just keep the iterable reference around.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants