From 192b46333194f8b85d5220c92ddeae9717809f38 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Thu, 5 Dec 2019 19:09:26 +0800 Subject: [PATCH] Detect if underlying transport is closing before write data to it --- libp2p/network/connection/raw_connection.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libp2p/network/connection/raw_connection.py b/libp2p/network/connection/raw_connection.py index fe09c6f..3e4d9af 100644 --- a/libp2p/network/connection/raw_connection.py +++ b/libp2p/network/connection/raw_connection.py @@ -23,6 +23,10 @@ class RawConnection(IRawConnection): self._drain_lock = asyncio.Lock() async def write(self, data: bytes) -> None: + # 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: