Skip to content

Commit 4882dd4

Browse files
committed
feat(sync-v2): Watchdog to detect stale syncing
1 parent d0a1ff2 commit 4882dd4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

hathor/p2p/sync_v2/agent.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ def __init__(self, protocol: 'HathorProtocol', reactor: Optional[Reactor] = None
148148
self._lc_run = LoopingCall(self.run_sync)
149149
self._lc_run.clock = self.reactor
150150
self._is_running = False
151+
self._sync_started_at: float = 0
152+
153+
# Maximum running time to consider a sync stale.
154+
self.max_running_time: int = 30 * 60 # seconds
151155

152156
# Whether we propagate transactions or not
153157
self._is_relaying = False
@@ -262,6 +266,16 @@ def handle_error(self, payload: str) -> None:
262266
def update_synced(self, synced: bool) -> None:
263267
self._synced = synced
264268

269+
def watchdog(self) -> None:
270+
"""Close connection if sync is stale."""
271+
if not self._is_running:
272+
return
273+
274+
dt = self.reactor.seconds() - self._sync_started_at
275+
if dt > self.max_running_time:
276+
self.log.warn('stale syncing detected, closing connection')
277+
self.protocol.send_error_and_close_connection('stale syncing')
278+
265279
@inlineCallbacks
266280
def run_sync(self) -> Generator[Any, Any, None]:
267281
""" Async step of the sync algorithm.
@@ -274,8 +288,10 @@ def run_sync(self) -> Generator[Any, Any, None]:
274288
if self._is_running:
275289
# Already running...
276290
self.log.debug('already running')
291+
self.watchdog()
277292
return
278293
self._is_running = True
294+
self._sync_started_at = self.reactor.seconds()
279295
try:
280296
yield self._run_sync()
281297
except Exception:

0 commit comments

Comments
 (0)