From d04634b57bbe7331abf9472e8e2d0b96de321083 Mon Sep 17 00:00:00 2001 From: Richard Kiss Date: Sun, 14 May 2017 13:43:15 -0700 Subject: [PATCH] Improve dht shutdown. Notice writing pauses. --- magneticod/magneticod/bittorrent.py | 7 +++++++ magneticod/magneticod/dht.py | 3 +++ 2 files changed, 10 insertions(+) diff --git a/magneticod/magneticod/bittorrent.py b/magneticod/magneticod/bittorrent.py index dce6336..f5784af 100644 --- a/magneticod/magneticod/bittorrent.py +++ b/magneticod/magneticod/bittorrent.py @@ -52,6 +52,7 @@ class DisposablePeer: self._metadata_future = loop.create_future() self._writer = None + self._is_paused = False try: self._reader, self._writer = await asyncio.open_connection( @@ -88,6 +89,12 @@ class DisposablePeer: self._writer.close() return self._metadata_future.result() + def pause_writing(self): + self._is_paused = True + + def resume_writing(self): + self._is_paused = False + def __on_message(self, message: bytes) -> None: length = len(message) diff --git a/magneticod/magneticod/dht.py b/magneticod/magneticod/dht.py index 79f64ac..bdd1f54 100644 --- a/magneticod/magneticod/dht.py +++ b/magneticod/magneticod/dht.py @@ -84,6 +84,9 @@ class SybilNode: if addr[1] == 0: return + if self._transport.is_closing(): + return + try: message = bencode.loads(data) except bencode.BencodeDecodingError: