From 97b3aca535e4d38b7bbed99267011327bf624ba8 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Mon, 4 Nov 2019 18:27:31 +0800 Subject: [PATCH] Fix: Force context switch before canceling swarm connection tasks --- libp2p/network/connection/swarm_connection.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libp2p/network/connection/swarm_connection.py b/libp2p/network/connection/swarm_connection.py index e25d75f0..29d544eb 100644 --- a/libp2p/network/connection/swarm_connection.py +++ b/libp2p/network/connection/swarm_connection.py @@ -43,6 +43,9 @@ class SwarmConn(INetConn): # We *could* optimize this but it really isn't worth it. for stream in self.streams: await stream.reset() + # Force context switch for stream handlers to process the stream reset event we just emit + # before we cancel the stream handler tasks. + await asyncio.sleep(0.1) for task in self._tasks: task.cancel()