Skip to content

Commit ac539e7

Browse files
gh-132917: fix data race on last_mem in free-threading gc (#134692)
1 parent a380d57 commit ac539e7

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Python/gc_free_threading.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@ gc_should_collect_mem_usage(GCState *gcstate)
20622062
// 70,000 new container objects.
20632063
return true;
20642064
}
2065-
Py_ssize_t last_mem = gcstate->last_mem;
2065+
Py_ssize_t last_mem = _Py_atomic_load_ssize_relaxed(&gcstate->last_mem);
20662066
Py_ssize_t mem_threshold = Py_MAX(last_mem / 10, 128);
20672067
if ((mem - last_mem) > mem_threshold) {
20682068
// The process memory usage has increased too much, do a collection.
@@ -2245,7 +2245,8 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
22452245

22462246
// Store the current memory usage, can be smaller now if breaking cycles
22472247
// freed some memory.
2248-
state->gcstate->last_mem = get_process_mem_usage();
2248+
Py_ssize_t last_mem = get_process_mem_usage();
2249+
_Py_atomic_store_ssize_relaxed(&state->gcstate->last_mem, last_mem);
22492250

22502251
// Append objects with legacy finalizers to the "gc.garbage" list.
22512252
handle_legacy_finalizers(state);

0 commit comments

Comments
 (0)