-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
enum_next and pairwise_next can result in tuple elements with zero reference count in free-threading build #121464
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There are more places in the codebase where the same issue occurs: cpython/Modules/itertoolsmodule.c Lines 360 to 367 in 58fdb16
cpython/Modules/itertoolsmodule.c Lines 3713 to 3736 in 58fdb16
Lines 246 to 253 in 126910e
Lines 246 to 253 in 126910e
Lines 196 to 209 in 126910e
Lines 2974 to 2994 in 58fdb16
|
It would be reasonable to |
@rhettinger Disabling the re-use of the tuple solves part of the issues with free-threading. We should also disable clearing the reference to the iterator (e.g. And finally we have to make sure we handle updates to cpython/Modules/itertoolsmodule.c Lines 382 to 383 in dd0ee20
cpython/Modules/itertoolsmodule.c Line 331 in dd0ee20
I think for pairwise we can do this with reasonably clean code.
For some other methods in cpython/Modules/itertoolsmodule.c Lines 2057 to 2075 in dd0ee20
I will think a bit more about alternative options. |
Bug report
Bug description:
The enumerate object (and also the
itertools.pairwise
since #118219) re-uses tuples when possible. It does this by checking the reference count to be 1:The refcount check and increment are not atomic, so in the free-threading build multiple threads can end up operating on the
result
object. It is possible that one thread already sets an item in the tuple, and another thread decrefs the item believing it is still an old item.In the nogil reference implementation (https://github.com/colesbury/nogil) no changes are made to address this. So maybe the issue cannot occur?
In #120591 this problem is addressed with a lock, which might be too much overhead (see the discussion in #120496)
@colesbury @hauntsaninja
CPython versions tested on:
CPython main branch
Operating systems tested on:
Windows
Linked PRs
The text was updated successfully, but these errors were encountered: