Skip to content

Commit 45281a2

Browse files
committed
feat(sync-v2): Watchdog to detect stale syncing
1 parent 9fff604 commit 45281a2

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
@@ -151,6 +151,10 @@ def __init__(self, protocol: 'HathorProtocol', reactor: Optional[Reactor] = None
151151
self._lc_run = LoopingCall(self.run_sync)
152152
self._lc_run.clock = self.reactor
153153
self._is_running = False
154+
self._sync_started_at: float = 0
155+
156+
# Maximum running time to consider a sync stale.
157+
self.max_running_time: int = 30 * 60 # seconds
154158

155159
# Whether we propagate transactions or not
156160
self._is_relaying = False
@@ -265,6 +269,16 @@ def handle_error(self, payload: str) -> None:
265269
def update_synced(self, synced: bool) -> None:
266270
self._synced = synced
267271

272+
def watchdog(self) -> None:
273+
"""Close connection if sync is stale."""
274+
if not self._is_running:
275+
return
276+
277+
dt = self.reactor.seconds() - self._sync_started_at
278+
if dt > self.max_running_time:
279+
self.log.warn('stale syncing detected, closing connection')
280+
self.protocol.send_error_and_close_connection('stale syncing')
281+
268282
@inlineCallbacks
269283
def run_sync(self) -> Generator[Any, Any, None]:
270284
""" Async step of the sync algorithm.
@@ -277,8 +291,10 @@ def run_sync(self) -> Generator[Any, Any, None]:
277291
if self._is_running:
278292
# Already running...
279293
self.log.debug('already running')
294+
self.watchdog()
280295
return
281296
self._is_running = True
297+
self._sync_started_at = self.reactor.seconds()
282298
try:
283299
yield self._run_sync()
284300
except Exception:

0 commit comments

Comments
 (0)