Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

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

Conversation

Yhg1s
Copy link
Member

@Yhg1sYhg1s commentedJan 8, 2025
edited
Loading

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 sharediterable (list, tuple or range) should be safe, not involve data races, and behave like iteration normally does.

  • Using a sharediterator should not crash or involve data races, and should only produce items regular iteration would produce. It isnot 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.

eendebakpt reacted with eyes emoji
concurrent iteration. (This is prep work for enabling specialization ofFOR_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.Providing stronger guarantees is possible for some of these iterators, butit's not always straight-forward and can significantly hamper the commoncase. Since iterators in general aren't shared between threads, and it'ssimply impossible to concurrently use many iterators (like generators),better to make sharing iterators without explicit synchronization clearlywrong.Specific issues fixed in order to make the tests pass: - List iteration could occasionally crash when a shared list wasn't already   marked as shared when reallocated. - 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. - Fast range iterators (for integers that fit in C longs) shared between   threads would sometimes produce values past the end of the range, because   the iterators use two bits of state that we can't efficiently update   atomically. Rewriting the iterators to have a single bit of state is   possible, but probably means more math for each iteration and may not be   worth it. - Long range iterators (for other numbers) shared between threads would   crash catastrophically in a variety of ways. This now uses a critical   section. Rewriting this to be more efficient is probably possible, but   since it deals with arbitrary Python objects it's difficult to get right.There seem to be no more exising races in list_get_item_ref, so drop it fromthe tsan suppression list.
actually correct and the real problem was an incorrect assert. The fast pathstill contains notionally unsafe uses of memcpy/memmove, so addlist_get_item_ref back to the TSan suppressions file.
iterators, and fix build failures because labels can't technically be at theend of compound statements (a statement must follow the label, even if it'sempty).
…len andtupleiter_setstate appropriately relaxed.
iterator test to accept the current behaviour of the fast range iterator andavoid testing the (very unsafe) long range iterator, use threading.Barrierwhere appropriate, and add comments to the unsafe uses of memcpy/memmove infree-threaded builds.
@Yhg1sYhg1s changed the titlegh-115999: Make list, tuple and range iteration more thread-safe.gh-115999: Make list and tuple iteration more thread-safe.Jan 12, 2025
@Yhg1s
Copy link
MemberAuthor

PTAL.

relaxed atomics. (These are not performance-sensitive parts of the code, andthis lets us detect races elsewhere involving the iterators.)
@Yhg1s
Copy link
MemberAuthor

@colesbury Do you still want to look at this?

Copy link
Contributor

@colesburycolesbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM

@Yhg1sYhg1s merged commit388e1ca intopython:mainFeb 19, 2025
45 checks passed
@Yhg1sYhg1s deleted the list-realloc branchFebruary 19, 2025 00:52
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@mpagempagempage left review comments

@eendebakpteendebakpteendebakpt left review comments

@corona10corona10corona10 left review comments

@colesburycolesburycolesbury approved these changes

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@Yhg1s@mpage@colesbury@eendebakpt@corona10

[8]ページ先頭

©2009-2025 Movatter.jp