Detect if underlying transport is closing before write data to it

This commit is contained in:
NIC619 2019-12-05 19:09:26 +08:00
parent 811cd7813a
commit 192b463331
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17

View File

@ -23,6 +23,10 @@ class RawConnection(IRawConnection):
self._drain_lock = asyncio.Lock() self._drain_lock = asyncio.Lock()
async def write(self, data: bytes) -> None: 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) self.writer.write(data)
# Reference: https://github.com/ethereum/lahja/blob/93610b2eb46969ff1797e0748c7ac2595e130aef/lahja/asyncio/endpoint.py#L99-L102 # noqa: E501 # 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: # Use a lock to serialize drain() calls. Circumvents this bug: