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 3
3
import contextlib
4
4
import enum
5
5
import functools
6
+ import gc
6
7
import inspect
7
8
import socket
8
9
import sys
@@ -488,7 +489,19 @@ def event_loop(request: "pytest.FixtureRequest") -> Iterator[asyncio.AbstractEve
488
489
"""Create an instance of the default event loop for each test case."""
489
490
loop = asyncio .get_event_loop_policy ().new_event_loop ()
490
491
yield loop
491
- loop .close ()
492
+ # Cleanup code copied from the implementation of asyncio.run()
493
+ try :
494
+ asyncio .runners ._cancel_all_tasks (loop )
495
+ loop .run_until_complete (loop .shutdown_asyncgens ())
496
+ if sys .version_info >= (3 , 9 ):
497
+ loop .run_until_complete (loop .shutdown_default_executor ())
498
+ finally :
499
+ loop .close ()
500
+ # Call the garbage collector to trigger ResourceWarning's as soon
501
+ # as possible (these are triggered in various __del__ methods).
502
+ # Without this, resources opened in one test can fail other tests
503
+ # when the warning is generated.
504
+ gc .collect ()
492
505
493
506
494
507
def _unused_port (socket_type : int ) -> int :
You can’t perform that action at this time.
0 commit comments