Skip to content

Commit

Permalink
Fix some warnings related to asyncio (#346)
Browse files Browse the repository at this point in the history
This PR fixes asyncio warnings about "RuntimeError: Event loop is closed" and "ConnectionResetError: Connection lost".
  • Loading branch information
borzunov authored Aug 3, 2021
1 parent f8d280e commit cf3f64a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion hivemind/p2p/p2p_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ async def _process_stream() -> None:
await P2P.send_protobuf(response, writer)
except Exception as e:
logger.warning("Exception while processing stream and sending responses:", exc_info=True)
await P2P.send_protobuf(RPCError(message=str(e)), writer)
# Sometimes `e` is a connection error, so we won't be able to report the error to the caller
with suppress(Exception):
await P2P.send_protobuf(RPCError(message=str(e)), writer)

with closing(writer):
processing_task = asyncio.create_task(_process_stream())
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import gc
import multiprocessing as mp
from contextlib import suppress
Expand All @@ -11,6 +12,22 @@
logger = get_logger(__name__)


@pytest.fixture
def event_loop():
"""
This overrides the ``event_loop`` fixture from pytest-asyncio
(e.g. to make it compatible with ``asyncio.subprocess``).
This fixture is identical to the original one but does not call ``loop.close()`` in the end.
Indeed, at this point, the loop is already stopped (i.e. next tests are free to create new loops).
However, finalizers of objects created in the current test may reference the current loop and fail if it is closed.
For example, this happens while using ``asyncio.subprocess`` (the ``asyncio.subprocess.Process`` finalizer
fails if the loop is closed, but works if the loop is only stopped).
"""

yield asyncio.get_event_loop()


@pytest.fixture(autouse=True, scope="session")
def cleanup_children():
yield
Expand Down

0 comments on commit cf3f64a

Please sign in to comment.