aio-libs/aiohttp

ResourceWarning: unclosed transport

Closed this issue · 2 comments

jwilk commented

Long story short

I'd like my software be warning-free.

Expected behaviour

Correct use of asyncio doesn't cause any warnings.

Actual behaviour

ResourceWarning: unclosed transport is emitted.

Steps to reproduce

Put this in test.py:

import asyncio
import aiohttp

async def main(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            await response.read()

url = 'https://example.org/'
loop = asyncio.get_event_loop()
loop.run_until_complete(main(url))
loop.close()

Then run:

$ python3 -Wd test.py 
/usr/lib/python3.5/asyncio/selector_events.py:582: ResourceWarning: unclosed transport <_SelectorSocketTransport fd=7>
  warnings.warn("unclosed transport %r" % self, ResourceWarning)

Your environment

aiohttp from git master (d8848dd), Python 3.5.2, Linux.

The issue cannot be solved, sorry.
The reason is: after calling transport.close() asyncio needs for extra loop iteration for actual closing.
It's by design.
So if you add await asyncio.sleep(0) just before loop closing -- warning disappears.

If you want -- please feel free to add a note section to aiohttp documentation for stressing the behavior.
Perhaps it should be somewhere at very end of http://aiohttp.readthedocs.io/en/stable/client.html

lock commented

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.