Skip to content

Commit

Permalink
refactor recursive to iterative approach of awaiting cancelled tasks (#…
Browse files Browse the repository at this point in the history
…484)

* refactor recursive to iterative approach of awaiting cancelled tasks

* close all tasks instead of one and rename loop method.

Co-authored-by: Randolph Busch <[email protected]>
  • Loading branch information
Randelung and Randolph Busch authored Mar 16, 2021
1 parent 3b30f43 commit 4eb0edb
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions asyncua/server/binary_server_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async def start(self):
self.hostname = sockname[0]
self.port = sockname[1]
self.logger.info('Listening on %s:%s', self.hostname, self.port)
self.cleanup_task = self.iserver.loop.create_task(self._await_closing_tasks())
self.cleanup_task = self.iserver.loop.create_task(self._close_task_loop())

async def stop(self):
self.logger.info('Closing asyncio socket server')
Expand All @@ -149,13 +149,18 @@ async def stop(self):
await self.cleanup_task
except asyncio.CancelledError:
pass
await self._await_closing_tasks(recursive=False)
await self._close_tasks()

if self._server:
self.iserver.loop.call_soon(self._server.close)
await self._server.wait_closed()

async def _await_closing_tasks(self, recursive=True):
async def _close_task_loop(self):
while True:
await self._close_tasks()
await asyncio.sleep(10)

async def _close_tasks(self):
while self.closing_tasks:
task = self.closing_tasks.pop()
try:
Expand All @@ -164,7 +169,4 @@ async def _await_closing_tasks(self, recursive=True):
# this means a stop request has been sent, it should not be catched
raise
except Exception:
logger.exception("Unexpected crash in BinaryServer._await_closing_tasks")
if recursive:
await asyncio.sleep(10)
await self._await_closing_tasks()
logger.exception("Unexpected crash in BinaryServer._close_tasks")

0 comments on commit 4eb0edb

Please sign in to comment.