From a32283450385f9820b5b7e7c98f39dc714f16727 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 25 Nov 2026 16:46:24 +0100 Subject: [PATCH] [3.14] gh-162616: Clear asyncio's `iscoroutine_typecache`s between refleak tests (GH-162617) * gh-162616: Clear asyncio's `iscoroutine_typecache`s between refleak tests Creating a task checks if the argument is a coroutine. For non-native coroutines, this adds the type (the test's `Wrapper`) to `_asyncio`'s `iscoroutine_typecache`. Another check adds it to `asyncio.coroutines._iscoroutine_typecache`. Both caches are capped at 100 entries, so refleak tests report a reference leak unless you run 100+ warmup runs. Clear both caches in `libregrtest.utils.clear_caches`. * Apply suggestions from code review --------- Co-authored-by: Andrew Svetlov (cherry picked from commit 233b95c00bd70f0bd60838b1cfc639996f1476a9) --- Lib/test/libregrtest/utils.py | 14 ++++++++++++++ Modules/_asynciomodule.c | 18 ++++++++++++++++++ Modules/clinic/_asynciomodule.c.h | 20 +++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 7012690cd83cdf..4f96edc8ad6ed8 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -307,6 +307,20 @@ def clear_caches(): else: importlib_metadata.FastPath.__new__.cache_clear() + try: + _asyncio = sys.modules['_asyncio'] + except KeyError: + pass + else: + _asyncio._clear_caches() + + try: + asyncio_coroutines = sys.modules['asyncio.coroutines'] + except KeyError: + pass + else: + asyncio_coroutines._iscoroutine_typecache.clear() + def get_build_info(): # Get most important configure and build options as a list of strings. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 43b59bb9af4b6c..6a23e606e8cc47 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -4222,6 +4222,23 @@ _asyncio_future_discard_from_awaited_by_impl(PyObject *module, PyObject *fut, Py_RETURN_NONE; } +/*[clinic input] +_asyncio._clear_caches + +Clear internal caches. Used for internal tests. +[clinic start generated code]*/ + +static PyObject * +_asyncio__clear_caches_impl(PyObject *module) +/*[clinic end generated code: output=9691a293f83d4303 input=6e1ae31e51dc906c]*/ +{ + asyncio_state *state = get_asyncio_state(module); + if (PySet_Clear(state->iscoroutine_typecache) < 0) { + return NULL; + } + Py_RETURN_NONE; +} + static int module_traverse(PyObject *mod, visitproc visit, void *arg) { @@ -4393,6 +4410,7 @@ static PyMethodDef asyncio_methods[] = { _ASYNCIO_ALL_TASKS_METHODDEF _ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF _ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF + _ASYNCIO__CLEAR_CACHES_METHODDEF {NULL, NULL} }; diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index d9202681492039..6ee49c336f0755 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -2257,4 +2257,22 @@ _asyncio_future_discard_from_awaited_by(PyObject *module, PyObject *const *args, exit: return return_value; } -/*[clinic end generated code: output=6a0d02bd421248aa input=a9049054013a1b77]*/ + +PyDoc_STRVAR(_asyncio__clear_caches__doc__, +"_clear_caches($module, /)\n" +"--\n" +"\n" +"Clear internal caches. Used for internal tests."); + +#define _ASYNCIO__CLEAR_CACHES_METHODDEF \ + {"_clear_caches", (PyCFunction)_asyncio__clear_caches, METH_NOARGS, _asyncio__clear_caches__doc__}, + +static PyObject * +_asyncio__clear_caches_impl(PyObject *module); + +static PyObject * +_asyncio__clear_caches(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _asyncio__clear_caches_impl(module); +} +/*[clinic end generated code: output=c4ad412387312d3a input=a9049054013a1b77]*/