Skip to content

Commit 39bec5a

Browse files
committed
raise PeerConnectionLost when peer dies in request
1 parent 9a4884c commit 39bec5a

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

trinity/protocol/common/managers.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ValidationError,
1818
)
1919

20+
from p2p.exceptions import PeerConnectionLost
2021
from p2p.peer import BasePeer, PeerSubscriber
2122
from p2p.protocol import (
2223
BaseRequest,
@@ -134,7 +135,7 @@ async def _run(self) -> None:
134135
async def _handle_msg(self, msg: TResponsePayload) -> None:
135136
if self.pending_request is None:
136137
self.logger.debug(
137-
"Got unexpected %s payload from %", self.response_msg_name, self._peer
138+
"Got unexpected %s payload from %s", self.response_msg_name, self._peer
138139
)
139140
return
140141

@@ -179,15 +180,24 @@ def _request(self, request: BaseRequest[TRequestPayload]) -> None:
179180
def _is_pending(self) -> bool:
180181
return self.pending_request is not None
181182

183+
async def _cleanup(self) -> None:
184+
if self.pending_request is not None:
185+
self.logger.debug("Stream shutting down, raising an exception on the pending request")
186+
_, future = self.pending_request
187+
future.set_exception(PeerConnectionLost("Pending request can't complete: peer is gone"))
188+
182189
def deregister_peer(self, peer: BasePeer) -> None:
183190
if self.pending_request is not None:
184-
self.logger.debug("Peer disconnected, trigger a timeout on the pending request")
191+
self.logger.debug("Peer disconnected, raising an exception on the pending request")
185192
_, future = self.pending_request
186-
future.set_exception(TimeoutError("Peer disconnected, simulating inevitable timeout"))
193+
future.set_exception(PeerConnectionLost("Pending request can't complete: peer is gone"))
187194

188195
def get_stats(self) -> Tuple[str, str]:
189196
return (self.response_msg_name, self.response_times.get_stats())
190197

198+
def __repr__(self) -> str:
199+
return f'<ResponseCandidateStream({self._peer!s}, {self.response_msg_type!r})>'
200+
191201

192202
class ExchangeManager(Generic[TRequestPayload, TResponsePayload, TResult]):
193203
_response_stream: ResponseCandidateStream[TRequestPayload, TResponsePayload] = None

trinity/sync/full/chain.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
cast,
1313
)
1414

15+
from cancel_token import OperationCancelled
1516
from cytoolz import (
1617
concat,
1718
merge,
@@ -28,7 +29,7 @@
2829
from eth.rlp.transactions import BaseTransaction
2930

3031
from p2p import protocol
31-
from p2p.exceptions import NoEligiblePeers
32+
from p2p.exceptions import NoEligiblePeers, PeerConnectionLost
3233
from p2p.protocol import Command
3334

3435
from trinity.db.chain import AsyncChainDB
@@ -204,7 +205,7 @@ async def _get_block_bodies(self,
204205
"""
205206
Requests the batch of block bodies from the given peer, returning the
206207
returned block bodies data and the headers for which block bodies were not
207-
returned for.
208+
returned.
208209
"""
209210
self.logger.debug("Requesting block bodies for %d headers from %s", len(batch), peer)
210211
try:
@@ -214,6 +215,16 @@ async def _get_block_bodies(self,
214215
"Timed out requesting block bodies for %d headers from %s", len(batch), peer,
215216
)
216217
return tuple(), batch
218+
except OperationCancelled:
219+
self.logger.trace("Pending block bodies call to %r cancelled", peer)
220+
return tuple(), batch
221+
except PeerConnectionLost:
222+
self.logger.debug("Peer went away, cancelling the block body request and moving on...")
223+
return tuple(), batch
224+
except Exception:
225+
self.logger.exception("Unknown error when getting block bodies")
226+
return tuple(), batch
227+
else:
217228
self.logger.debug(
218229
"Got block bodies for %d headers from %s", len(block_body_bundles), peer,
219230
)
@@ -306,6 +317,15 @@ async def _get_receipts(self,
306317
"Timed out requesting receipts for %d headers from %s", len(batch), peer,
307318
)
308319
return tuple(), batch
320+
except OperationCancelled:
321+
self.logger.trace("Pending receipts call to %r cancelled", peer)
322+
return tuple(), batch
323+
except PeerConnectionLost:
324+
self.logger.debug("Peer went away, cancelling the receipts request and moving on...")
325+
return tuple(), batch
326+
except Exception:
327+
self.logger.exception("Unknown error when getting receipts")
328+
return tuple(), batch
309329
else:
310330
self.logger.debug(
311331
"Got receipts for %d headers from %s", len(receipt_bundles), peer,

0 commit comments

Comments
 (0)