Skip to content

Commit 49b1fb4

Browse files
authored
gh-142048: Fix lost gc allocations count on thread cleanup (#142233)
1 parent 7067126 commit 49b1fb4

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
@@ -1765,16 +1765,23 @@ PyThreadState_Clear(PyThreadState *tstate)
17651765
struct _Py_freelists *freelists = _Py_freelists_GET();
17661766
_PyObject_ClearFreeLists(freelists, 1);
17671767

1768+
// Flush the thread's local GC allocation count to the global count
1769+
// before the thread state is cleared, otherwise the count is lost.
1770+
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
1771+
_Py_atomic_add_int(&tstate->interp->gc.young.count,
1772+
(int)tstate_impl->gc.alloc_count);
1773+
tstate_impl->gc.alloc_count = 0;
1774+
17681775
// Merge our thread-local refcounts into the type's own refcount and
17691776
// free our local refcount array.
1770-
_PyObject_FinalizePerThreadRefcounts((_PyThreadStateImpl *)tstate);
1777+
_PyObject_FinalizePerThreadRefcounts(tstate_impl);
17711778

17721779
// Remove ourself from the biased reference counting table of threads.
17731780
_Py_brc_remove_thread(tstate);
17741781

17751782
// Release our thread-local copies of the bytecode for reuse by another
17761783
// thread
1777-
_Py_ClearTLBCIndex((_PyThreadStateImpl *)tstate);
1784+
_Py_ClearTLBCIndex(tstate_impl);
17781785
#endif
17791786

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

0 commit comments

Comments
 (0)