From 7ae9de90023960cff99c558dc86a1902d1ca6361 Mon Sep 17 00:00:00 2001 From: mhchia Date: Tue, 4 Feb 2020 17:09:26 +0800 Subject: [PATCH] Fix handler in `net_stream_pair_factory` Change it to async function. It wasn't discovered since we caught all exceptions raised in stream handlers. --- libp2p/tools/factories.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libp2p/tools/factories.py b/libp2p/tools/factories.py index 84e6af39..67e26519 100644 --- a/libp2p/tools/factories.py +++ b/libp2p/tools/factories.py @@ -404,13 +404,18 @@ async def net_stream_pair_factory( stream_1: INetStream - # Just a proxy, we only care about the stream - def handler(stream: INetStream) -> None: + # Just a proxy, we only care about the stream. + # Add a barrier to avoid stream being removed. + event_handler_finished = trio.Event() + + async def handler(stream: INetStream) -> None: nonlocal stream_1 stream_1 = stream + await event_handler_finished.wait() async with host_pair_factory(is_secure) as hosts: hosts[1].set_stream_handler(protocol_id, handler) stream_0 = await hosts[0].new_stream(hosts[1].get_id(), [protocol_id]) yield stream_0, stream_1 + event_handler_finished.set()