@@ -2001,10 +2001,12 @@ gc_should_collect(GCState *gcstate)
20012001{
20022002 int count = _Py_atomic_load_int_relaxed (& gcstate -> young .count );
20032003 int threshold = gcstate -> young .threshold ;
2004- int gc_enabled = _Py_atomic_load_int_relaxed (& gcstate -> enabled );
2005- int pause_count = _Py_atomic_load_int_relaxed (
2006- & gcstate -> automatic_collection_pause_count );
2007- if (count <= threshold || threshold == 0 || !gc_enabled || pause_count ) {
2004+ if (count <= threshold || threshold == 0 ) {
2005+ return false;
2006+ }
2007+ if (!_Py_atomic_load_int_relaxed (& gcstate -> enabled ) ||
2008+ _Py_atomic_load_int_relaxed (
2009+ & gcstate -> automatic_collection_pause_count )) {
20082010 return false;
20092011 }
20102012 if (gcstate -> old [0 ].threshold == 0 ) {
@@ -2073,8 +2075,7 @@ gc_collect_internal(PyInterpreterState *interp,
20732075{
20742076 _PyEval_StopTheWorld (interp );
20752077
2076- // A concurrent deferral may begin after this collection has emitted its
2077- // start notification, but it must take effect before any heap traversal.
2078+ // Close the race with a deferral that started before the world stopped.
20782079 if (state -> reason == _Py_GC_REASON_HEAP &&
20792080 _Py_atomic_load_int (
20802081 & state -> gcstate -> automatic_collection_pause_count )) {
@@ -2246,8 +2247,6 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
22462247 s -> object_stats .object_visits = 0 ;
22472248 }
22482249#endif
2249- GC_STAT_ADD (generation , collections , 1 );
2250-
22512250 if (reason != _Py_GC_REASON_SHUTDOWN ) {
22522251 invoke_gc_callback (tstate , "start" , generation , 0 , 0 , 0 , 0.0 );
22532252 }
@@ -2282,6 +2281,7 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
22822281 _Py_atomic_store_int (& gcstate -> collecting , 0 );
22832282 return 0 ;
22842283 }
2284+ GC_STAT_ADD (generation , collections , 1 );
22852285
22862286 m = state .collected ;
22872287 n = state .uncollectable ;
0 commit comments