Skip to content

httpx\_client.py", line 1991, in __del__ AttributeError: 'NoneType' object has no attribute 'OPENED' #2024

Closed

Description

@kusime

The starting point for issues should usually be a discussion...

https://github.com/encode/httpx/discussions

Possible bugs may be raised as a "Potential Issue" discussion, feature requests may be raised as an "Ideas" discussion. We can then determine if the discussion needs to be escalated into an "Issue" or not.

This will help us ensure that the "Issues" list properly reflects ongoing or needed work on the project.


  • Initially raised as discussion #...

Sender Runtime Finished ...
[<Response [200 OK]>, <Response [200 OK]>, <Response [200 OK]>, <Response [200 OK]>]
Exception ignored in: <function AsyncClient.del at 0x000001C2BCC04A60>
Traceback (most recent call last):
File "C:\ANACONNA\lib\site-packages\httpx_client.py", line 1991, in del
AttributeError: 'NoneType' object has no attribute 'OPENED'

__ If I Delete The __del__ function in _client.py , my code can work without error, but if not ,the error above will just happend..
And my function can get The coroutine data return , but I still don know why this error happend?
Is Python garbage Recycle effect this ? __

And The __del__ code is

    def __del__(self) -> None:
        # We use 'getattr' here, to manage the case where '__del__()' is called
        # on a partially initiallized instance that raised an exception during
        # the call to '__init__()'.
        if getattr(self, "_state", None) == ClientState.OPENED:  # noqa: B009
            # Unlike the sync case, we cannot silently close the client when
            # it is garbage collected, because `.aclose()` is an async operation,
            # but `__del__` is not.
            #
            # For this reason we require explicit close management for
            # `AsyncClient`, and issue a warning on unclosed clients.
            #
            # The context managed style is usually preferable, because it neatly
            # ensures proper resource cleanup:
            #
            #     async with httpx.AsyncClient() as client:
            #         ...
            #
            # However, an explicit call to `aclose()` is also sufficient:
            #
            #    client = httpx.AsyncClient()
            #    try:
            #        ...
            #    finally:
            #        await client.aclose()
            warnings.warn(
                f"Unclosed {self!r}. "
                "See https://www.python-httpx.org/async/#opening-and-closing-clients "
                "for details."
            )


Activity

  1. lovelydinosaur commented on Jan 11, 2022

    @lovelydinosaur
    Member

    See also #1951

    I'm not able to reproduce this, and it's very odd to me.
    What's the simplest possible case you can come up with that reproduces this issue?

  2. kusime commented on Jan 12, 2022

    @kusime
    Author

    thank you for you reply,here is the code can reproduce this issue

    run.py

    import asyncio
    import httpx
    
    class run():
        def __init__(self, URL) -> None:
            print("init_ run class")
            self.ret = "client get return"
            self.URL = URL
            self.Client = self.return_client()
            self.Return = 'Return'
    
        def go(self):
            asyncio.run(self.run_client())
            return self.ret
    
        def return_client(self):
            Client = httpx.AsyncClient()
            return Client
    
        async def send(self):
            self.Return = await self.Client.get(self.URL)
    
        async def run_client(self):
            Task = asyncio.create_task(self.send())
            await Task
            await self.Client.aclose()
    
    

    test.py

    import run
    Instance = run.run('https://www.bing.com')
    print(Instance.go())
    

    then run the test.py, can reproduce the error

    init_ run class
    client get return
    Exception ignored in: <function AsyncClient.__del__ at 0x0000021E388B09D0>
    Traceback (most recent call last):
      File "C:\ANACONNA\lib\site-packages\httpx\_client.py", line 1991, in __del__
    AttributeError: 'NoneType' object has no attribute 'OPENED'
    

    By the way ,if you run

    Instance = run('https://www.bing.com')
    print(Instance.go())
    

    after run.py ,the error will not be raise.

    init_ run class
    client get return
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions