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

Calling Connection.close from __del__ method could result in RuntimeWarning #1211

Closed
1 task done
m-novikov opened this issue Nov 19, 2021 · 5 comments
Closed
1 task done
Labels

Comments

@m-novikov
Copy link
Contributor

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
@m-novikov m-novikov added the bug label Nov 19, 2021
@m-novikov
Copy link
Contributor Author

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.

@m-novikov
Copy link
Contributor Author

Duplicate of #1115 I suppose

@Andrew-Chen-Wang
Copy link
Collaborator

Yes but an implementation that resolves this comment would be great: #1115 (comment)

@Ivo-Donchev
Copy link

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:/

@Andrew-Chen-Wang
Copy link
Collaborator

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants