Calling Connection.close from __del__ method could result in RuntimeWarning
m-novikov opened this issue · 5 comments
Describe the bug
Discovered this bug in testing. After establishing connection to redis if reference was garbage collected not awaited task could stay in even loop.
To Reproduce
# Copy code to test_warn.py and run
# python -m pytest test_warn.py
import gc
import asyncio
async def test_connection_runtime_warning():
redis = from_url(url="redis://127.0.0.1:6379")
await redis.ping()
del redis
gc.collect()
Expected behavior
No runtime warnings, managing lifecycle of object should be explicit as it's not possible to reason if or when __del__
going to be called.
Logs/tracebacks
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<Connection.disconnect() running at ../aioredis-py/aioredis/connection.py:764> wait_for=<Future pending cb=[<TaskWakeupMe
thWrapper object at 0x7f3a0043c070>()]>>
### Python Version
```console
$ python --version
3.9
aioredis Version
$ python -m pip show aioredis
Version: 2.0
Additional context
No response
Code of Conduct
- I agree to follow the aio-libs Code of Conduct
One option would be to add an explicit async cleanup method, which is once called will close the connection setting the flag that this class was cleaned up. In turn in __del__
method if this flag is set it should simply return.
Yes but an implementation that resolves this comment would be great: #1115 (comment)
I could confirm it's still an issue.
We're using it in production as an internal dependency for websockets library (aioredis-py version 1.3.1) and it's causing issues from time to time:/
@m-novikov hopefully #1227 resolved this; let me know if I'm missing anything else.
@Ivo-Donchev the initial issue is talking about aioredis 2.0.0, not 1.3.1, which is a completely new rework of aioredis based on redis-py. I am also aware of some random tasks hanging in my aioredis 1.3.1 instances. Something that could help debugging would be to see if the number of connected clients to a single Redis db grows over time. Profiling memory could also help.