Added socket destructor to unregister the socket from the loop

This commit is contained in:
nocturn9x 2021-04-22 14:31:58 +02:00
parent dfcac651b4
commit d0394ed7d8
1 changed files with 11 additions and 0 deletions

View File

@ -79,6 +79,17 @@ class AsyncSocket:
raise ResourceClosed("I/O operation on closed socket")
await self.loop.connect_sock(self.sock, addr)
def __del__(self):
"""
Implements the destructor for the async socket,
notifying the event loop that the socket must not
be listened for anymore. This avoids the loop
blocking forever on trying to read from a socket
that's gone out of scope without being closed
"""
self.loop.selector.unregister(self.sock)
async def __aenter__(self):
return self