From 45ef63af2040cfc404d89301f50104c53e00fe4f Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Mon, 23 Dec 2019 09:24:41 +0100 Subject: [PATCH 1/5] bump multiaddr from setup to 0.0.9 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 356487a..2d0b6f3 100644 --- a/setup.py +++ b/setup.py @@ -66,7 +66,7 @@ install_requires = [ "pycryptodome>=3.9.2,<4.0.0", "base58>=1.0.3,<2.0.0", "pymultihash>=0.8.2", - "multiaddr>=0.0.8,<0.1.0", + "multiaddr>=0.0.9,<0.1.0", "rpcudp>=3.0.0,<4.0.0", "lru-dict>=1.1.6", "protobuf>=3.10.0,<4.0.0", From 8f52315816046852953f0f7c72d754218a731811 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Tue, 24 Dec 2019 16:19:49 +0800 Subject: [PATCH 2/5] Fix `close` an already reset connection --- libp2p/network/connection/raw_connection.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libp2p/network/connection/raw_connection.py b/libp2p/network/connection/raw_connection.py index 4f01b12..bbc41b2 100644 --- a/libp2p/network/connection/raw_connection.py +++ b/libp2p/network/connection/raw_connection.py @@ -55,4 +55,8 @@ class RawConnection(IRawConnection): self.writer.close() if sys.version_info < (3, 7): return - await self.writer.wait_closed() + try: + await self.writer.wait_closed() + # In case the connection is already reset. + except (ConnectionResetError, RawConnError): + return From a390d213851460787f8032c5fa01794855a3c891 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Tue, 24 Dec 2019 21:57:13 +0800 Subject: [PATCH 3/5] Remove `RawConnError` from exception handling --- libp2p/network/connection/raw_connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libp2p/network/connection/raw_connection.py b/libp2p/network/connection/raw_connection.py index bbc41b2..69d8796 100644 --- a/libp2p/network/connection/raw_connection.py +++ b/libp2p/network/connection/raw_connection.py @@ -58,5 +58,5 @@ class RawConnection(IRawConnection): try: await self.writer.wait_closed() # In case the connection is already reset. - except (ConnectionResetError, RawConnError): + except ConnectionResetError: return From c62f1f374f3027ec8759ac78bd57013a71fd2037 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Tue, 24 Dec 2019 22:23:38 +0800 Subject: [PATCH 4/5] Check if transport is closing before write/close --- libp2p/network/connection/raw_connection.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libp2p/network/connection/raw_connection.py b/libp2p/network/connection/raw_connection.py index 69d8796..dddbd99 100644 --- a/libp2p/network/connection/raw_connection.py +++ b/libp2p/network/connection/raw_connection.py @@ -26,10 +26,11 @@ class RawConnection(IRawConnection): async def write(self, data: bytes) -> None: """Raise `RawConnError` if the underlying connection breaks.""" - try: - self.writer.write(data) - except ConnectionResetError as error: - raise RawConnError(error) + # Detect if underlying transport is closing before write data to it + # ref: https://github.com/ethereum/trinity/pull/614 + if self.writer.transport.is_closing(): + raise ConnectionResetError("Transport is closing") + self.writer.write(data) # Reference: https://github.com/ethereum/lahja/blob/93610b2eb46969ff1797e0748c7ac2595e130aef/lahja/asyncio/endpoint.py#L99-L102 # noqa: E501 # Use a lock to serialize drain() calls. Circumvents this bug: # https://bugs.python.org/issue29930 @@ -52,6 +53,8 @@ class RawConnection(IRawConnection): raise RawConnError(error) async def close(self) -> None: + if self.writer.transport.is_closing(): + return self.writer.close() if sys.version_info < (3, 7): return From 3b0386d86176f3ba615c9afad1001a67d37473ea Mon Sep 17 00:00:00 2001 From: NIC619 Date: Tue, 24 Dec 2019 22:27:16 +0800 Subject: [PATCH 5/5] Raise `RawConnError` --- libp2p/network/connection/raw_connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libp2p/network/connection/raw_connection.py b/libp2p/network/connection/raw_connection.py index dddbd99..f8ee6bb 100644 --- a/libp2p/network/connection/raw_connection.py +++ b/libp2p/network/connection/raw_connection.py @@ -29,7 +29,7 @@ class RawConnection(IRawConnection): # Detect if underlying transport is closing before write data to it # ref: https://github.com/ethereum/trinity/pull/614 if self.writer.transport.is_closing(): - raise ConnectionResetError("Transport is closing") + raise RawConnError("Transport is closing") self.writer.write(data) # Reference: https://github.com/ethereum/lahja/blob/93610b2eb46969ff1797e0748c7ac2595e130aef/lahja/asyncio/endpoint.py#L99-L102 # noqa: E501 # Use a lock to serialize drain() calls. Circumvents this bug: