-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Sequence iterator thread-safety #120496
Comments
I suspect we might want something faster than just locking the iterator in this case. Locking might slow down iteration quite a bit. Will wait for Sam to hear his ideas. |
Btw, the race is rangeiter_next (and all the other iterator protocols) racing with itself. Here's a sample:
|
In the section Container Thread-Safety, PEP 703 mentions fast paths for |
Thanks for the link! I don't think we can do the same thing for the |
I don't think we want to fix this -- the performance trade-off is not worth it. We should instead document that iterators aren't thread-safe -- that they won't be guaranteed to return unique elements if accessed concurrently from multiple threads. |
+1 |
I think that we can close the issue after some documentation. @eendebakpt , AFAIK, you are working on several other cases, but if your works are related to thread-safe of iterators, I think that Sam's opinion is more reasonable for me. |
I am worried that not making it thread safe will lead to some very hard to track down bugs. Besides the risk of returning unexpected elements (maybe it is enough to document this, although I am personally not convinced yet), there is also the risks of overflows inside the C code (that will happen for the reversed iteration). Regardless of the final decision, I will benchmark the |
(cherry picked from commit 7e189ae) Co-authored-by: Donghee Na <donghee.na@python.org>
@colesbury If I understand correctly we want the container iterators to be thread-safe in the sense that iterating using multiple threads will not crash the interpreter, but it is ok to be not thread-safe in the sense that the iterator can return results that would under single-threading be considered a bug. For example when using multiple threads the iteration over That was very surprising for me as a user! I did read PEP 703 again, and I believe the behavior is correctly described in the section on container thread-safety. Also the documentation update by @corona10 will help. Nevertheless, I am wondering if we can somehow make users more aware of this. For example by providing explicit examples in documentation like https://github.com/python/cpython/blob/main/Doc/howto/free-threading-extensions.rst? |
Yes
Yes, I agree. I've started working on a free-threading howto for Python. |
Bug report
Bug description:
Sequence iterators are not thread-safe under the free-threaded build. They'll sometimes be accessed with the same index.
Here is a minimal repro:
I also tested it with
list
anddict
iterators as well as a custom class implementing__getitem__
.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux, macOS
Linked PRs
The text was updated successfully, but these errors were encountered: