Skip to content

Commit 9388518

Browse files
authored
Fix allocation count decrement and GC state update
Adjust allocation count handling to prevent negative values and update GC state accordingly.
1 parent b1a6c16 commit 9388518

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

Python/gc_free_threading.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,10 +2207,23 @@ record_deallocation(PyThreadState *tstate)
22072207
{
22082208
struct _gc_thread_state *gc = &((_PyThreadStateImpl *)tstate)->gc;
22092209

2210-
// Only decrement if positive, matching gc.c behavior which prevents
2211-
// negative counts (see PyObject_GC_Del in gc.c).
2212-
if (gc->alloc_count > 0) {
2213-
gc->alloc_count--;
2210+
gc->alloc_count--;
2211+
if (gc->alloc_count <= -LOCAL_ALLOC_COUNT_THRESHOLD) {
2212+
GCState *gcstate = &tstate->interp->gc;
2213+
int count = _Py_atomic_load_int_relaxed(&gcstate->young.count);
2214+
int new_count;
2215+
do {
2216+
if (count == 0){
2217+
break;
2218+
}
2219+
new_count = count + (int)gc->alloc_count;
2220+
if (new_count < 0) {
2221+
new_count = 0;
2222+
}
2223+
} while (!_Py_atomic_compare_exchange_int(&gcstate->young.count,
2224+
&count,
2225+
new_count));
2226+
gc->alloc_count = 0;
22142227
}
22152228
}
22162229

0 commit comments

Comments
 (0)