Better cancel all outstanding tasks.

This commit is contained in:
Richard Kiss 2017-05-14 13:55:19 -07:00
parent d04634b57b
commit 73d97d8188
2 changed files with 8 additions and 10 deletions

View File

@ -51,7 +51,7 @@ def main():
node = dht.SybilNode(arguments.node_addr, complete_info_hashes, arguments.max_metadata_size) node = dht.SybilNode(arguments.node_addr, complete_info_hashes, arguments.max_metadata_size)
loop.run_until_complete(node.launch(loop)) loop.run_until_complete(node.launch(loop))
loop.create_task(watch_q(database, node._metadata_q)) watch_q_task = loop.create_task(watch_q(database, node._metadata_q))
try: try:
loop.run_forever() loop.run_forever()
@ -59,7 +59,9 @@ def main():
logging.critical("Keyboard interrupt received! Exiting gracefully...") logging.critical("Keyboard interrupt received! Exiting gracefully...")
finally: finally:
database.close() database.close()
watch_q_task.cancel()
loop.run_until_complete(node.shutdown()) loop.run_until_complete(node.shutdown())
loop.run_until_complete(watch_q_task)
return 0 return 0

View File

@ -105,15 +105,11 @@ class SybilNode:
self.__n_max_neighbours = self.__n_max_neighbours * 101 // 100 self.__n_max_neighbours = self.__n_max_neighbours * 101 // 100
async def shutdown(self) -> None: async def shutdown(self) -> None:
peers = [peer for peer in itertools.chain.from_iterable(self.__peers.values())] futures = [peer for peer in itertools.chain.from_iterable(self.__peers.values())]
for peer in peers: futures.extend(self._tasks)
peer.cancel() for future in futures:
for peer in peers: future.cancel()
await peer await asyncio.wait(futures)
for task in self._tasks:
task.cancel()
for task in self._tasks:
await task
self._transport.close() self._transport.close()
def __on_FIND_NODE_response(self, message: bencode.KRPCDict) -> None: def __on_FIND_NODE_response(self, message: bencode.KRPCDict) -> None: