bpo-40887: Don't use finalized free lists (GH-20700) · python/cpython@bcb1983
@@ -1430,6 +1430,11 @@ void
14301430_PyAsyncGen_Fini(PyThreadState *tstate)
14311431{
14321432_PyAsyncGen_ClearFreeLists(tstate);
1433+#ifdef Py_DEBUG
1434+struct _Py_async_gen_state *state = &tstate->interp->async_gen;
1435+state->value_numfree = -1;
1436+state->asend_numfree = -1;
1437+#endif
14331438}
1434143914351440@@ -1474,6 +1479,10 @@ async_gen_asend_dealloc(PyAsyncGenASend *o)
14741479Py_CLEAR(o->ags_sendval);
14751480PyInterpreterState *interp = _PyInterpreterState_GET();
14761481struct _Py_async_gen_state *state = &interp->async_gen;
1482+#ifdef Py_DEBUG
1483+// async_gen_asend_dealloc() must not be called after _PyAsyncGen_Fini()
1484+assert(state->asend_numfree != -1);
1485+#endif
14771486if (state->asend_numfree < _PyAsyncGen_MAXFREELIST) {
14781487assert(PyAsyncGenASend_CheckExact(o));
14791488state->asend_freelist[state->asend_numfree++] = o;
@@ -1632,6 +1641,10 @@ async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
16321641PyAsyncGenASend *o;
16331642PyInterpreterState *interp = _PyInterpreterState_GET();
16341643struct _Py_async_gen_state *state = &interp->async_gen;
1644+#ifdef Py_DEBUG
1645+// async_gen_asend_new() must not be called after _PyAsyncGen_Fini()
1646+assert(state->asend_numfree != -1);
1647+#endif
16351648if (state->asend_numfree) {
16361649state->asend_numfree--;
16371650o = state->asend_freelist[state->asend_numfree];
@@ -1667,6 +1680,10 @@ async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
16671680Py_CLEAR(o->agw_val);
16681681PyInterpreterState *interp = _PyInterpreterState_GET();
16691682struct _Py_async_gen_state *state = &interp->async_gen;
1683+#ifdef Py_DEBUG
1684+// async_gen_wrapped_val_dealloc() must not be called after _PyAsyncGen_Fini()
1685+assert(state->value_numfree != -1);
1686+#endif
16701687if (state->value_numfree < _PyAsyncGen_MAXFREELIST) {
16711688assert(_PyAsyncGenWrappedValue_CheckExact(o));
16721689state->value_freelist[state->value_numfree++] = o;
@@ -1737,6 +1754,10 @@ _PyAsyncGenValueWrapperNew(PyObject *val)
1737175417381755PyInterpreterState *interp = _PyInterpreterState_GET();
17391756struct _Py_async_gen_state *state = &interp->async_gen;
1757+#ifdef Py_DEBUG
1758+// _PyAsyncGenValueWrapperNew() must not be called after _PyAsyncGen_Fini()
1759+assert(state->value_numfree != -1);
1760+#endif
17401761if (state->value_numfree) {
17411762state->value_numfree--;
17421763o = state->value_freelist[state->value_numfree];