gmolau
gmolau

Reputation: 3005

Unclosed client session warning of unknown origin

I'm using a third-party API client library that is based on aiohttp. The library doesn't implement it's clients as context managers, but that should be no problem as long as I always close the aiohttp.ClientSession manually (right?).

The problem is that even after carefully doing this I still get the Unclosed client session warning without any information from where it is coming from. I'm capturing the warnings traceback with pytest -W error::ResourceWarning, but the result is simply

.Exception ignored in: <bound method ClientSession.__del__ of <aiohttp.client.ClientSession object at 0x10fc15ba8>>
Traceback (most recent call last):
  File "<PROJECT>/venv/lib/python3.6/site-packages/aiohttp/client.py", line 211, in __del__
    **kwargs)
ResourceWarning: Unclosed client session <aiohttp.client.ClientSession object at 0x10fc15ba8>

i.e. a traceback with a single frame.

Can someone tell me what I'm doing wrong here and how I can get more information about that warning?

Upvotes: 2

Views: 2311

Answers (2)

artemonsh
artemonsh

Reputation: 123

It is not an error, it is just a warning. It comes from a __del__ function in ClientSession (github). It means that the client got an WSMsgType.CLOSE message from the server.

If you are using websockets and the function ws_connect (aiohttp docs) It is handled automatically by the argument autoclose=True. You could set it to false and see what will happen. You could simply turn off the Internet and then turn it on and see a new response in the console with WSMsgType.CLOSE message.

Upvotes: 1

Andrew Svetlov
Andrew Svetlov

Reputation: 17386

Run your code in debug mode.

Set PYTHONASYNCIODEBUG=1 environment variable to see the extended information.

In particular, aiohttp logs a traceback to the line that was used to creating unclosed ClientSession object.

Upvotes: 5

Related Questions