-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
gh-129533: Update PyGC_Enable/Disable/IsEnabled to use atomic operation #129563
Conversation
corona10
commented
Feb 2, 2025
•
edited by bedevere-app
bot
Loading
edited by bedevere-app
bot
- Issue: Race on GC enabled state under free threading #129533
TSAN is passed at my local machine:
|
Python/gc_free_threading.c
Outdated
int old_state = gcstate->enabled; | ||
gcstate->enabled = 0; | ||
int old_state = _Py_atomic_load_int(&(gcstate->enabled)); | ||
while (!_Py_atomic_compare_exchange_int(&gcstate->enabled, &old_state, 0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to update old_state explicitly since __atomic_compare_exchange_n automatically handle.
https://gcc.gnu.org/onlinedocs/gcc-8.4.0/gcc/_005f_005fatomic-Builtins.html
cc @hawkinsp |
Co-authored-by: mpage <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Python/gc_free_threading.c
Outdated
@@ -1952,25 +1953,23 @@ int | |||
PyGC_Enable(void) | |||
{ | |||
GCState *gcstate = get_gc_state(); | |||
int old_state = gcstate->enabled; | |||
gcstate->enabled = 1; | |||
int old_state = _Py_atomic_exchange_int(&gcstate->enabled, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the old_state variable. Same remark in PyGC_Disable() and PyGC_IsEnabled().
Waiting @pablogsal before merging this PR :) |
Thanks @corona10 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Sorry, @corona10, I could not cleanly backport this to
|
…operation (pythongh-129563) (cherry picked from commit b184abf)
GH-129756 is a backport of this pull request to the 3.13 branch. |