Skip to content

(doc) Mention requiring closing Reddit http connection #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Andrew-Chen-Wang
Copy link
Contributor

@Andrew-Chen-Wang Andrew-Chen-Wang commented May 2, 2025

I noticed the Reddit instance has an __aexit__ method that should auto close, but it's no where mentioned in the docs.

You'll get an error without explicitly closing the connection if you were to follow the get started docs

The docs in general are not really up to date either. The tutorials are also a bit outdated:

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x105e36900>
Unclosed connector
connections: ['deque([(<aiohttp.client_proto.ResponseHandler object at 0x105e98d70>, 436028.214513125)])']
connector: <aiohttp.connector.TCPConnector object at 0x105e36a50>
Exception ignored in: <function _DeleteDummyThreadOnDel.__del__ at 0x100d794e0>
Traceback (most recent call last):
  File "/Users/andrew/.local/share/mise/installs/python/3.13.3/lib/python3.13/threading.py", line 1383, in __del__
TypeError: 'NoneType' object does not support the context manager protocol

Usage of __del__ for auto closing in async code bases

There is some kind of class in this lib that's setting __del__ and calling close() that should be deleted from the code base.

Even adding .close() shows

Exception ignored in: <function _DeleteDummyThreadOnDel.__del__ at 0x1006b54e0>
Traceback (most recent call last):
  File "/Users/andrew/.local/share/mise/installs/python/3.13.3/lib/python3.13/threading.py", line 1383, in __del__
TypeError: 'NoneType' object does not support the context manager protocol

It's noted that trying to close the connection using __del__ doesn't work here in the redis-py thread:

https://github.com/redis/redis-py/blob/120517f88671469888a747d8e1ca1d1c2348b301/redis/asyncio/connection.py#L227

   def __del__(self, _warnings: Any = warnings):
        # For some reason, the individual streams don't get properly garbage
        # collected and therefore produce no resource warnings.  We add one
        # here, in the same style as those from the stdlib.
        if getattr(self, "_writer", None):
            _warnings.warn(
                f"unclosed Connection {self!r}", ResourceWarning, source=self
            )

            try:
                asyncio.get_running_loop()
                self._close()
            except RuntimeError:
                # No actions been taken if pool already closed.
                pass

The problem ends up being the event loop is already closed at the end of the program sometimes (sometimes it's still open; it's a non-zero chance essentially). redis/redis-py#2999


reddit = asyncpraw.Reddit(...)
await reddit.close()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add using Reddit as an asynchronous context manager? I think there is an example in the docs for the Reddit class. If not, here's this:

async with asyncpraw.Reddit(...) as reddit:
    print(await reddit.user.me()

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

Successfully merging this pull request may close these issues.

2 participants