File tree Expand file tree Collapse file tree 1 file changed +14
-1
lines changed Expand file tree Collapse file tree 1 file changed +14
-1
lines changed Original file line number Diff line number Diff line change 33import contextlib
44import enum
55import functools
6+ import gc
67import inspect
78import socket
89import sys
@@ -484,7 +485,19 @@ def event_loop(request: "pytest.FixtureRequest") -> Iterator[asyncio.AbstractEve
484485 """Create an instance of the default event loop for each test case."""
485486 loop = asyncio .get_event_loop_policy ().new_event_loop ()
486487 yield loop
487- loop .close ()
488+ # Cleanup code copied from the implementation of asyncio.run()
489+ try :
490+ asyncio .runners ._cancel_all_tasks (loop )
491+ loop .run_until_complete (loop .shutdown_asyncgens ())
492+ if sys .version_info >= (3 , 9 ):
493+ loop .run_until_complete (loop .shutdown_default_executor ())
494+ finally :
495+ loop .close ()
496+ # Call the garbage collector to trigger ResourceWarning's as soon
497+ # as possible (these are triggered in various __del__ methods).
498+ # Without this, resources opened in one test can fail other tests
499+ # when the warning is generated.
500+ gc .collect ()
488501
489502
490503def _unused_port (socket_type : int ) -> int :
You can’t perform that action at this time.
0 commit comments