Skip to content

gh-132917: fix data race on last_mem in free-threading gc #134692

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

Merged
merged 2 commits into from
May 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ gc_should_collect_mem_usage(GCState *gcstate)
// 70,000 new container objects.
return true;
}
Py_ssize_t last_mem = gcstate->last_mem;
Py_ssize_t last_mem = _Py_atomic_load_ssize_relaxed(&gcstate->last_mem);
Py_ssize_t mem_threshold = Py_MAX(last_mem / 10, 128);
if ((mem - last_mem) > mem_threshold) {
// The process memory usage has increased too much, do a collection.
Expand Down Expand Up @@ -2245,7 +2245,8 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,

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

// Append objects with legacy finalizers to the "gc.garbage" list.
handle_legacy_finalizers(state);
Expand Down
Loading