Skip to content

Commit 60768bc

Browse files
encukouasvetlov
andcommitted
[3.15] gh-162616: Clear asyncio's iscoroutine_typecaches 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 <andrew.svetlov@gmail.com> (cherry picked from commit 233b95c)
1 parent 9ad66bb commit 60768bc

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎Lib/test/libregrtest/utils.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,20 @@ def noop_search_function(name):
326326
codecs.register(noop_search_function)
327327
codecs.unregister(noop_search_function)
328328

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

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

‎Modules/_asynciomodule.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,6 +4207,23 @@ _asyncio_future_discard_from_awaited_by_impl(PyObject *module, PyObject *fut,
42074207
Py_RETURN_NONE;
42084208
}
42094209

4210+
/*[clinic input]
4211+
_asyncio._clear_caches
4212+
4213+
Clear internal caches. Used for internal tests.
4214+
[clinic start generated code]*/
4215+
4216+
static PyObject *
4217+
_asyncio__clear_caches_impl(PyObject *module)
4218+
/*[clinic end generated code: output=9691a293f83d4303 input=6e1ae31e51dc906c]*/
4219+
{
4220+
asyncio_state *state = get_asyncio_state(module);
4221+
if (PySet_Clear(state->iscoroutine_typecache) < 0) {
4222+
return NULL;
4223+
}
4224+
Py_RETURN_NONE;
4225+
}
4226+
42104227
static int
42114228
module_traverse(PyObject *mod, visitproc visit, void *arg)
42124229
{
@@ -4378,6 +4395,7 @@ static PyMethodDef asyncio_methods[] = {
43784395
_ASYNCIO_ALL_TASKS_METHODDEF
43794396
_ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF
43804397
_ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF
4398+
_ASYNCIO__CLEAR_CACHES_METHODDEF
43814399
{NULL, NULL}
43824400
};
43834401

‎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)