Skip to content

Commit 233b95c

Browse files
encukouasvetlov
andauthored
gh-162616: Clear asyncio's iscoroutine_typecaches between refleak tests (#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>
1 parent 4b66b87 commit 233b95c

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
@@ -329,6 +329,20 @@ 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()
345+
332346

333347
def get_build_info():
334348
# 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
@@ -4218,6 +4218,23 @@ _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+
42214238
static int
42224239
module_traverse(PyObject *mod, visitproc visit, void *arg)
42234240
{
@@ -4389,6 +4406,7 @@ static PyMethodDef asyncio_methods[] = {
43894406
_ASYNCIO_ALL_TASKS_METHODDEF
43904407
_ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF
43914408
_ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF
4409+
_ASYNCIO__CLEAR_CACHES_METHODDEF
43924410
{NULL, NULL}
43934411
};
43944412

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