Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Remove __del__ from Redis (Fixes #1115) (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Chen-Wang committed Dec 16, 2021
1 parent 5062740 commit 7f65c4c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES/1227.bugfix
@@ -0,0 +1 @@
Change `__del__` in Redis to raise ResourceWarning (Fixes #1115)
31 changes: 12 additions & 19 deletions aioredis/client.py
Expand Up @@ -1055,15 +1055,17 @@ async def __aenter__(self: _RedisT) -> _RedisT:
async def __aexit__(self, exc_type, exc_value, traceback):
await self.close()

def __del__(self):
try:
loop = asyncio.get_event_loop()
if loop.is_running():
loop.create_task(self.close())
else:
loop.run_until_complete(self.close())
except Exception:
pass
_DEL_MESSAGE = "Unclosed Redis client"

def __del__(self, _warnings: Any = warnings) -> None:
if self.connection is not None:
_warnings.warn(
f"Unclosed client session {self!r}",
ResourceWarning,
source=self,
)
context = {"client": self, "message": self._DEL_MESSAGE}
asyncio.get_event_loop().call_exception_handler(context)

async def close(self):
conn = self.connection
Expand Down Expand Up @@ -4350,16 +4352,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
def __await__(self):
return self._async_self().__await__()

def __del__(self):
try:
loop = asyncio.get_event_loop()
if loop.is_running():
loop.create_task(self.reset())
else:
loop.run_until_complete(self.reset())
super().__del__()
except Exception:
pass
_DEL_MESSAGE = "Unclosed Pipeline client"

def __len__(self):
return len(self.command_stack)
Expand Down

0 comments on commit 7f65c4c

Please sign in to comment.