From 94c2560a1e4407258c59cb137d11bc2724611eaa Mon Sep 17 00:00:00 2001 From: Tin Tvrtkovic Date: Tue, 20 Apr 2021 01:04:59 +0200 Subject: [PATCH] More robust loop replacement --- pytest_asyncio/plugin.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 80cce291..7665ff4d 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -91,9 +91,13 @@ def pytest_fixture_setup(fixturedef, request): outcome = yield loop = outcome.get_result() policy = asyncio.get_event_loop_policy() - old_loop = policy.get_event_loop() - if old_loop is not loop: - old_loop.close() + try: + old_loop = policy.get_event_loop() + if old_loop is not loop: + old_loop.close() + except RuntimeError: + # Swallow this, since it's probably bad event loop hygiene. + pass policy.set_event_loop(loop) return