Skip to content

Commit aab8642

Browse files
committed
An async socket now closes the write stream (synchronously) when it is deleted.
A StreamWriter does not reliably close its socket when it is collected.
1 parent e6a84f4 commit aab8642

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

redis/asyncio/connection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ def __repr__(self):
207207
repr_args = ",".join((f"{k}={v}" for k, v in self.repr_pieces()))
208208
return f"{self.__class__.__name__}<{repr_args}>"
209209

210+
def __del__(self):
211+
self._close_socket()
212+
210213
@abstractmethod
211214
def repr_pieces(self):
212215
pass
@@ -377,6 +380,15 @@ async def disconnect(self, nowait: bool = False) -> None:
377380
f"Timed out closing connection after {self.socket_connect_timeout}"
378381
) from None
379382

383+
def _close_socket(self):
384+
"""Close the socket directly. Used during garbage collection to
385+
make sure the underlying socket is released. This does not happen
386+
reliably when the stream is garbage collected.
387+
"""
388+
if self._writer:
389+
if os.getpid() == self.pid:
390+
self._writer.close()
391+
380392
async def _send_ping(self):
381393
"""Send PING, expect PONG in return"""
382394
await self.send_command("PING", check_health=False)

0 commit comments

Comments
 (0)