Skip to content

Commit ad21f73

Browse files
pipermerriamgsalgado
authored andcommitted
Handle in state downloader background task (#1167)
1 parent 868346d commit ad21f73

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

trinity/sync/full/state.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
HexaryTrieSync,
6363
SyncRequest,
6464
)
65-
65+
from trinity.utils.cancellation import async_handle_cancellation
6666
from trinity.utils.timer import Timer
6767

6868

@@ -234,9 +234,20 @@ async def request_nodes(self, node_keys: Iterable[Hash32]) -> None:
234234
self.request_tracker.active_requests[peer] = (time.time(), batch)
235235
asyncio.ensure_future(self._request_and_process_nodes(peer, batch))
236236

237+
@async_handle_cancellation
237238
async def _request_and_process_nodes(self, peer: ETHPeer, batch: Tuple[Hash32, ...]) -> None:
238239
self.logger.debug("Requesting %d trie nodes from %s", len(batch), peer)
239-
node_data = await peer.requests.get_node_data(batch)
240+
try:
241+
node_data = await peer.requests.get_node_data(batch)
242+
except TimeoutError as err:
243+
self.logger.debug(
244+
"Timed out waiting for %s trie nodes from %s: %s",
245+
len(batch),
246+
peer,
247+
err,
248+
)
249+
node_data = tuple()
250+
240251
try:
241252
self.request_tracker.active_requests.pop(peer)
242253
except KeyError:

trinity/utils/cancellation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import functools
2+
from typing import (
3+
Any,
4+
Awaitable,
5+
Callable,
6+
TypeVar,
7+
Optional,
8+
)
9+
10+
from cancel_token import OperationCancelled
11+
12+
13+
TReturn = TypeVar('TReturn')
14+
15+
16+
def async_handle_cancellation(awaitable_fn: Callable[..., Awaitable[TReturn]]
17+
) -> Callable[..., Awaitable[Optional[TReturn]]]:
18+
async def inner(*args: Any, **kwargs: Any) -> Optional[TReturn]:
19+
try:
20+
return await awaitable_fn(*args, **kwargs)
21+
except OperationCancelled:
22+
return None
23+
24+
return functools.update_wrapper(inner, awaitable_fn)

0 commit comments

Comments
 (0)