https://github.com/python/cpython/commit/2ba1aed596ff9e7c5e193cf990f1f20f08bbf116
commit: 2ba1aed596ff9e7c5e193cf990f1f20f08bbf116
branch: main
author: Alex Turner <[email protected]>
committer: colesbury <[email protected]>
date: 2024-04-29T20:26:26Z
summary:

gh-117657: TSAN fix race on `gstate->young.count` (#118313)

files:
M Python/gc_free_threading.c
M Tools/tsan/suppressions_free_threading.txt

diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index 8c0940d8f066a7..ef6aaad1252311 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -1044,9 +1044,20 @@ record_deallocation(PyThreadState *tstate)
 }
 
 static void
-gc_collect_internal(PyInterpreterState *interp, struct collection_state *state)
+gc_collect_internal(PyInterpreterState *interp, struct collection_state 
*state, int generation)
 {
     _PyEval_StopTheWorld(interp);
+
+    // update collection and allocation counters
+    if (generation+1 < NUM_GENERATIONS) {
+        state->gcstate->old[generation].count += 1;
+    }
+
+    state->gcstate->young.count = 0;
+    for (int i = 1; i <= generation; ++i) {
+        state->gcstate->old[i-1].count = 0;
+    }
+
     // merge refcounts for all queued objects
     merge_all_queued_objects(interp, state);
     process_delayed_frees(interp);
@@ -1115,7 +1126,6 @@ gc_collect_internal(PyInterpreterState *interp, struct 
collection_state *state)
 static Py_ssize_t
 gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
 {
-    int i;
     Py_ssize_t m = 0; /* # objects collected */
     Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
     PyTime_t t1 = 0;   /* initialize to prevent a compiler warning */
@@ -1161,15 +1171,6 @@ gc_collect_main(PyThreadState *tstate, int generation, 
_PyGC_Reason reason)
         PyDTrace_GC_START(generation);
     }
 
-    /* update collection and allocation counters */
-    if (generation+1 < NUM_GENERATIONS) {
-        gcstate->old[generation].count += 1;
-    }
-    gcstate->young.count = 0;
-    for (i = 1; i <= generation; i++) {
-        gcstate->old[i-1].count = 0;
-    }
-
     PyInterpreterState *interp = tstate->interp;
 
     struct collection_state state = {
@@ -1177,7 +1178,7 @@ gc_collect_main(PyThreadState *tstate, int generation, 
_PyGC_Reason reason)
         .gcstate = gcstate,
     };
 
-    gc_collect_internal(interp, &state);
+    gc_collect_internal(interp, &state, generation);
 
     m = state.collected;
     n = state.uncollectable;
diff --git a/Tools/tsan/suppressions_free_threading.txt 
b/Tools/tsan/suppressions_free_threading.txt
index 4b1a2fdf6dd43a..4f6648a7573184 100644
--- a/Tools/tsan/suppressions_free_threading.txt
+++ b/Tools/tsan/suppressions_free_threading.txt
@@ -27,7 +27,6 @@ race:_PyObject_GC_TRACK
 race:_PyParkingLot_Park
 race:_PyType_HasFeature
 race:assign_version_tag
-race:gc_collect_main
 race:gc_restore_tid
 race:initialize_new_array
 race:insertdict

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to