From fda856f368e2abc323b2fda6e0aa356aec907db0 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Thu, 11 Dec 2025 15:07:38 +0530 Subject: [PATCH] =?utf8?q?[3.13]=20gh-142048:=20Fix=20lost=20gc=20allocati?= =?utf8?q?ons=20count=20on=20thread=20cleanup=20(GH=E2=80=A6=20(#142506)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit [3.13] gh-142048: Fix lost gc allocations count on thread cleanup (GH-142233) (cherry picked from commit 49b1fb43f65290dadeb83ed6f7c0c74995fda7a1) Co-authored-by: Kevin Wang --- Python/pystate.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Python/pystate.c b/Python/pystate.c index ba62a93d4574..68e76e4aed5a 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1743,6 +1743,14 @@ PyThreadState_Clear(PyThreadState *tstate) // Remove ourself from the biased reference counting table of threads. _Py_brc_remove_thread(tstate); + + // Flush the thread's local GC allocation count to the global count + // before the thread state is cleared, otherwise the count is lost. + _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate; + _Py_atomic_add_int(&tstate->interp->gc.generations[0].count, + (int)tstate_impl->gc.alloc_count); + tstate_impl->gc.alloc_count = 0; + #endif // Merge our queue of pointers to be freed into the interpreter queue. -- 2.47.3