Skip to content

Commit 0114178

Browse files
[3.14] gh-142048: Fix lost gc allocations count on thread cleanup (GH-142233) (#142504)
gh-142048: Fix lost gc allocations count on thread cleanup (GH-142233) (cherry picked from commit 49b1fb4) Co-authored-by: Kevin Wang <[email protected]>
1 parent d14697d commit 0114178

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Python/pystate.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,16 +1815,23 @@ PyThreadState_Clear(PyThreadState *tstate)
18151815
struct _Py_freelists *freelists = _Py_freelists_GET();
18161816
_PyObject_ClearFreeLists(freelists, 1);
18171817

1818+
// Flush the thread's local GC allocation count to the global count
1819+
// before the thread state is cleared, otherwise the count is lost.
1820+
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
1821+
_Py_atomic_add_int(&tstate->interp->gc.young.count,
1822+
(int)tstate_impl->gc.alloc_count);
1823+
tstate_impl->gc.alloc_count = 0;
1824+
18181825
// Merge our thread-local refcounts into the type's own refcount and
18191826
// free our local refcount array.
1820-
_PyObject_FinalizePerThreadRefcounts((_PyThreadStateImpl *)tstate);
1827+
_PyObject_FinalizePerThreadRefcounts(tstate_impl);
18211828

18221829
// Remove ourself from the biased reference counting table of threads.
18231830
_Py_brc_remove_thread(tstate);
18241831

18251832
// Release our thread-local copies of the bytecode for reuse by another
18261833
// thread
1827-
_Py_ClearTLBCIndex((_PyThreadStateImpl *)tstate);
1834+
_Py_ClearTLBCIndex(tstate_impl);
18281835
#endif
18291836

18301837
// Merge our queue of pointers to be freed into the interpreter queue.

0 commit comments

Comments
 (0)