Skip to content

Commit 65d8923

Browse files
asvetlovencukou
andauthored
[3.14] gh-162616: Clear asyncio's iscoroutine_typecaches between refleak tests (GH-162617) (#162683)
* 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 --------- (cherry picked from commit 233b95c) Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 465d2fb commit 65d8923

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
@@ -307,6 +307,20 @@ def clear_caches():
307307
else:
308308
importlib_metadata.FastPath.__new__.cache_clear()
309309

310+
try:
311+
_asyncio = sys.modules['_asyncio']
312+
except KeyError:
313+
pass
314+
else:
315+
_asyncio._clear_caches()
316+
317+
try:
318+
asyncio_coroutines = sys.modules['asyncio.coroutines']
319+
except KeyError:
320+
pass
321+
else:
322+
asyncio_coroutines._iscoroutine_typecache.clear()
323+
310324

311325
def get_build_info():
312326
# 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
@@ -4222,6 +4222,23 @@ _asyncio_future_discard_from_awaited_by_impl(PyObject *module, PyObject *fut,
42224222
Py_RETURN_NONE;
42234223
}
42244224

4225+
/*[clinic input]
4226+
_asyncio._clear_caches
4227+
4228+
Clear internal caches. Used for internal tests.
4229+
[clinic start generated code]*/
4230+
4231+
static PyObject *
4232+
_asyncio__clear_caches_impl(PyObject *module)
4233+
/*[clinic end generated code: output=9691a293f83d4303 input=6e1ae31e51dc906c]*/
4234+
{
4235+
asyncio_state *state = get_asyncio_state(module);
4236+
if (PySet_Clear(state->iscoroutine_typecache) < 0) {
4237+
return NULL;
4238+
}
4239+
Py_RETURN_NONE;
4240+
}
4241+
42254242
static int
42264243
module_traverse(PyObject *mod, visitproc visit, void *arg)
42274244
{
@@ -4393,6 +4410,7 @@ static PyMethodDef asyncio_methods[] = {
43934410
_ASYNCIO_ALL_TASKS_METHODDEF
43944411
_ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF
43954412
_ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF
4413+
_ASYNCIO__CLEAR_CACHES_METHODDEF
43964414
{NULL, NULL}
43974415
};
43984416

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