-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
enhance docs for critical sections #137334
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
Conversation
The other danger we might want to mention is what we have been calling "re-entrancy". That's not quite the accurate word for it since re-entrancy is only one type of problem of that kind. The more general problem is that you call a Python API and it ends up mutating state that you didn't expect to change. I.e. modifying "pre-conditions" your logic sequence is depending on. Also known as "changing things under you" or "spooky action at a distance". In Python, there are many surprising ways this can happen. When I was working on thread-safety for typeobject.c, I discovered some that surprised me, even as a long-time Python user. A few examples:
Using a critical section (kind of obviously) doesn't protect you from this kind of thing. Why would it when this same problem can happen even if you never have more than a single thread. If it did try to protect you then likely the program would deadlock. I guess "potentially re-entrant" would be an okay description of this. I.e. if you call this API, it can basically do anything since it can start executing Python-level code (via a hash method, finalizer, etc). Then, it could very well re-enter the same function or method. |
I have changed the wording now to just any calls to C API which covers this. |
Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
(cherry picked from commit 5ae8b97) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
(cherry picked from commit 5ae8b97) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
GH-138167 is a backport of this pull request to the 3.14 branch. |
GH-138168 is a backport of this pull request to the 3.13 branch. |
📚 Documentation preview 📚: https://cpython-previews--137334.org.readthedocs.build/