Skip to content

Commit

Permalink
tcp: Adjust to Python 3.12 for clean shutdown
Browse files Browse the repository at this point in the history
Python 3.12 has changed its behavior[1] such that wait_closed() doesn't
return before all open connections are closed -- and we used to block on
that before we even attempted to shut down the individual connections.

[1]: python/cpython#104344
  • Loading branch information
chrysn committed Sep 20, 2023
1 parent c08d4fe commit 7238ed7
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions aiocoap/transports/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,19 @@ async def shutdown(self):
self.log.debug("Shutting down server %r", self)
self._tokenmanager = None
self.server.close()
# This should be quick, no need to make a task of it -- and now all
# previously accepted connections should be in to receive their proper
# release
await self.server.wait_closed()
# Since server has been closed, we won't be getting any *more*
# connections, so we can process them all now:
shutdowns = [
asyncio.create_task(
c.release(),
**py38args(name="Close client %s" % c))
for c
in self._pool
]
if not shutdowns:
# wait is documented to require a non-empty set
return
shutdowns.append(asyncio.create_task(
self.server.wait_closed(),
**py38args(name="Close server %s" % self)))
# There is at least one member, so we can just .wait()
await asyncio.wait(shutdowns)

class TCPClient(_TCPPooling, interfaces.TokenInterface):
Expand Down

0 comments on commit 7238ed7

Please sign in to comment.