Skip to content

Commit

Permalink
Fix shutdown (#28)
Browse files Browse the repository at this point in the history
Python 3.12 changed Server.wait_closed to actually wait for the server to close, which revealed that our shutdown logic was incorrect. We need to first allow the queue to flush and close the connections, then wait.
  • Loading branch information
jkeljo authored Feb 18, 2024
1 parent e87c309 commit af28223
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions greeneye/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,31 +693,32 @@ def _create_protocol(self) -> GemProtocol:

async def close(self) -> None:
if self._server is not None:
LOG.info(
"Closing server on {}".format(self._server.sockets[0].getsockname())
)
sockname = self._server.sockets[0].getsockname()
LOG.info("Closing the server on {}...".format(sockname))
# Disallow new connections
self._server.close()

# Wait for packets to be processed
await self._queue.join()

if self._consumer_task is not None:
# Cancel consumer task
self._consumer_task.cancel()
try:
await self._consumer_task
except asyncio.CancelledError:
pass
self._consumer_task = None

while len(self._protocols) > 0:
(_, protocol) = self._protocols.popitem()
protocol.close()

# Wait for shutdown
await self._server.wait_closed()
self._server = None

# Wait for packets to be processed
await self._queue.join()

if self._consumer_task is not None:
# Cancel consumer task
self._consumer_task.cancel()
try:
await self._consumer_task
except asyncio.CancelledError:
pass
self._consumer_task = None

while len(self._protocols) > 0:
(_, protocol) = self._protocols.popitem()
protocol.close()
LOG.info("Closed the server on {}.".format(sockname))


MonitorListener = Union[Callable[[Monitor], Awaitable[None]], Callable[[Monitor], None]]
Expand Down

0 comments on commit af28223

Please sign in to comment.