Skip to content

Conversation

kumaraditya303
Copy link
Contributor

@kumaraditya303 kumaraditya303 commented Aug 3, 2025

@bedevere-app bedevere-app bot added the docs Documentation in the Doc dir label Aug 3, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Aug 3, 2025
@kumaraditya303 kumaraditya303 marked this pull request as ready for review August 4, 2025 10:33
@nascheme
Copy link
Member

nascheme commented Aug 8, 2025

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:

  • __hash__ methods on custom types that are used as dictionary keys
  • comparison methods on custom types that somewhere get compared
  • __getattr__ and __getattribute__ methods
  • Py_DECREF(), due to finalizers

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.

@kumaraditya303
Copy link
Contributor Author

The other danger we might want to mention is what we have been calling "re-entrancy".

I have changed the wording now to just any calls to C API which covers this.

@kumaraditya303 kumaraditya303 added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Aug 26, 2025
@kumaraditya303 kumaraditya303 merged commit 5ae8b97 into python:main Aug 26, 2025
31 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Aug 26, 2025
@miss-islington-app
Copy link

Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@kumaraditya303 kumaraditya303 deleted the cs-docs branch August 26, 2025 17:13
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 26, 2025
(cherry picked from commit 5ae8b97)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 26, 2025
(cherry picked from commit 5ae8b97)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@bedevere-app
Copy link

bedevere-app bot commented Aug 26, 2025

GH-138167 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Aug 26, 2025
@bedevere-app
Copy link

bedevere-app bot commented Aug 26, 2025

GH-138168 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Aug 26, 2025
kumaraditya303 added a commit that referenced this pull request Aug 26, 2025
enhance docs for critical sections (GH-137334)
(cherry picked from commit 5ae8b97)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
hugovk pushed a commit that referenced this pull request Aug 27, 2025
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants