Skip to content

Commit 43e1a33

Browse files
committed
gh-162616: Clear asyncio's iscoroutine_typecaches 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`.
1 parent ea7b1da commit 43e1a33

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎Lib/test/libregrtest/utils.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,19 @@ def noop_search_function(name):
329329
codecs.register(noop_search_function)
330330
codecs.unregister(noop_search_function)
331331

332+
try:
333+
_asyncio = sys.modules['_asyncio']
334+
except KeyError:
335+
pass
336+
else:
337+
_asyncio._clear_caches()
338+
339+
try:
340+
asyncio_coroutines = sys.modules['asyncio.coroutines']
341+
except KeyError:
342+
pass
343+
else:
344+
asyncio_coroutines._iscoroutine_typecache.clear()
332345

333346
def get_build_info():
334347
# Get most important configure and build options as a list of strings.

‎Modules/_asynciomodule.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,6 +4218,24 @@ _asyncio_future_discard_from_awaited_by_impl(PyObject *module, PyObject *fut,
42184218
Py_RETURN_NONE;
42194219
}
42204220

4221+
/*[clinic input]
4222+
_asyncio._clear_caches
4223+
4224+
Clear internal caches. Used for internal tests.
4225+
[clinic start generated code]*/
4226+
4227+
static PyObject *
4228+
_asyncio__clear_caches_impl(PyObject *module)
4229+
/*[clinic end generated code: output=9691a293f83d4303 input=6e1ae31e51dc906c]*/
4230+
{
4231+
asyncio_state *state = get_asyncio_state(module);
4232+
if (PySet_Clear(state->iscoroutine_typecache) < 0) {
4233+
return NULL;
4234+
}
4235+
Py_RETURN_NONE;
4236+
}
4237+
4238+
42214239
static int
42224240
module_traverse(PyObject *mod, visitproc visit, void *arg)
42234241
{
@@ -4389,6 +4407,7 @@ static PyMethodDef asyncio_methods[] = {
43894407
_ASYNCIO_ALL_TASKS_METHODDEF
43904408
_ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF
43914409
_ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF
4410+
_ASYNCIO__CLEAR_CACHES_METHODDEF
43924411
{NULL, NULL}
43934412
};
43944413

‎Modules/clinic/_asynciomodule.c.h‎

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)