gh-142048: Fix lost gc allocations count on thread cleanup (#142233)
Some checks are pending
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / iOS (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run

This commit is contained in:
Kevin Wang 2025-12-10 02:29:40 -05:00 committed by GitHub
parent 70671267c1
commit 49b1fb43f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1765,16 +1765,23 @@ PyThreadState_Clear(PyThreadState *tstate)
struct _Py_freelists *freelists = _Py_freelists_GET();
_PyObject_ClearFreeLists(freelists, 1);
// 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.young.count,
(int)tstate_impl->gc.alloc_count);
tstate_impl->gc.alloc_count = 0;
// Merge our thread-local refcounts into the type's own refcount and
// free our local refcount array.
_PyObject_FinalizePerThreadRefcounts((_PyThreadStateImpl *)tstate);
_PyObject_FinalizePerThreadRefcounts(tstate_impl);
// Remove ourself from the biased reference counting table of threads.
_Py_brc_remove_thread(tstate);
// Release our thread-local copies of the bytecode for reuse by another
// thread
_Py_ClearTLBCIndex((_PyThreadStateImpl *)tstate);
_Py_ClearTLBCIndex(tstate_impl);
#endif
// Merge our queue of pointers to be freed into the interpreter queue.