Skip to content

Commit da77101

Browse files
committed
Fix lost allocations on thread cleanup
1 parent db098a4 commit da77101

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
@@ -1741,16 +1741,23 @@ PyThreadState_Clear(PyThreadState *tstate)
17411741
struct _Py_freelists *freelists = _Py_freelists_GET();
17421742
_PyObject_ClearFreeLists(freelists, 1);
17431743

1744+
// Flush the thread's local GC allocation count to the global count
1745+
// before the thread state is cleared, otherwise the count is lost.
1746+
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
1747+
_Py_atomic_add_int(&tstate->interp->gc.young.count,
1748+
(int)tstate_impl->gc.alloc_count);
1749+
tstate_impl->gc.alloc_count = 0;
1750+
17441751
// Merge our thread-local refcounts into the type's own refcount and
17451752
// free our local refcount array.
1746-
_PyObject_FinalizePerThreadRefcounts((_PyThreadStateImpl *)tstate);
1753+
_PyObject_FinalizePerThreadRefcounts(tstate_impl);
17471754

17481755
// Remove ourself from the biased reference counting table of threads.
17491756
_Py_brc_remove_thread(tstate);
17501757

17511758
// Release our thread-local copies of the bytecode for reuse by another
17521759
// thread
1753-
_Py_ClearTLBCIndex((_PyThreadStateImpl *)tstate);
1760+
_Py_ClearTLBCIndex(tstate_impl);
17541761
#endif
17551762

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

0 commit comments

Comments
 (0)