Skip to content

gh-153928: make Unicode iterators thread-safe - #155873

Open
BHUVANSH855 wants to merge 4 commits into
python:mainfrom
BHUVANSH855:fix-153928-unicode-iterator-thread-safety
Open

BHUVANSH855 wants to merge 4 commits into
python:mainfrom
BHUVANSH855:fix-153928-unicode-iterator-thread-safety

Conversation

@BHUVANSH855

@BHUVANSH855 BHUVANSH855 commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix data races in Unicode iterators in free-threaded builds.

This makes it_index accesses atomic in both Unicode iterator implementations and atomically exchanges it_seq when the iterator is exhausted so that only one thread releases the iterator-owned reference.

__length_hint__ and __reduce__ also take a strong reference to the underlying string under a critical section before accessing it.

Adds free-threading regression tests covering shared string iteration and concurrent iterator exhaustion.

Fixes issue gh-153928.

Comment thread Objects/unicodeobject.c Outdated
@BHUVANSH855
BHUVANSH855 requested a review from picnixz August 15, 2026 21:46
@BHUVANSH855
BHUVANSH855 marked this pull request as ready for review August 15, 2026 21:51
@BHUVANSH855

Copy link
Copy Markdown
Contributor Author

@picnixz gentle ping !

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not confident enough here. I am afraid that this could slightly affect the regular build if because of PGO not being able to do something.

Since the problem is also not just necessarily for str/bytes but for any iterable family, I would rather defer the decision to @kumaraditya303 for that.

Comment thread Objects/unicodeobject.c Outdated
Comment thread Objects/unicodeobject.c Outdated
Comment thread Objects/unicodeobject.c Outdated
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.

2 participants