Skip to content
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

ClientSession.close() is a coroutine warning #2209

Closed
iselind opened this issue Aug 16, 2017 · 10 comments
Closed

ClientSession.close() is a coroutine warning #2209

iselind opened this issue Aug 16, 2017 · 10 comments
Labels
Milestone

Comments

@iselind
Copy link

iselind commented Aug 16, 2017

Long story short

I get a deprecation warning in aiohttp 2.2.5 which mess up Vim's quickfix window

Expected behaviour

No deprecation warning

Actual behaviour

The code seems to work but i get this deprecation warning each time i run my tests.

/usr/local/opt/Python-3.5.2/lib/python3.5/site-packages/aiohttp/helpers.py:139: DeprecationWarning: ClientSession.close() is a coroutine
  warnings.warn(self._msg, DeprecationWarning)

My tests are based on the unittest package from Python stdlib.

Steps to reproduce

I run tests using AioHTTPTestCase and unittest_run_loop from aiohttp.test_utils

I don't have any line that close any session in my tests so i guess that the warning comes from the aiohttp library.

Your environment

% pip3 freeze | grep aio                
aio-pika==0.18.0
aiodns==1.1.1
aiohttp==2.2.5

I'm running Python 3.5.2 using async and await syntax.

@asvetlov
Copy link
Member

Thus you need add lines for explicit session closing maybe?

@iselind
Copy link
Author

iselind commented Aug 18, 2017 via email

@asvetlov
Copy link
Member

Do you use plain aiohttp.request()?

@asvetlov asvetlov reopened this Aug 18, 2017
@iselind
Copy link
Author

iselind commented Aug 18, 2017

My tests look more like this as an example

        request = await self.client.request("GET", "/api/v1/targets")          
        self.assertEqual(request.status, 200)                                  
        text = await request.text()                                            
        self.assertEqual("[]", text)

I guess that self.client.request could be a aiohttp.request under the hood. Attaching part of my code base to hopefully clarify what i'm doing in test.zip

Searching through my code base for the string session, disregarding case, comes up with nothing.

@iselind
Copy link
Author

iselind commented Aug 18, 2017

I can see if i can scale down the code enough to get the issue unless you beat me to it.

@iselind
Copy link
Author

iselind commented Aug 18, 2017

Interestingly enough it seems to be enough with the code in the testing documentation to trigger the error. There is no need to satisfy test tests. It just warn immediately. Attaching the code i tried just now.

See testcode.zip

What i get when i run is

$ python3 -m unittest
/usr/local/lib/python3.5/dist-packages/aiohttp/helpers.py:139: DeprecationWarning: ClientSession.close() is a coroutine
  warnings.warn(self._msg, DeprecationWarning)
E
======================================================================
ERROR: test_example (test_foo.MyAppTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
[...]
NameError: name 'root' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.043s

FAILED (errors=1)

Notice the deprecation warning in the beginning of the output. So i cannot see there is anything weird with what I'm doing.

I seem to get the Deprecation waring because _CoroGuard wasn't awaited upon. This is discovered in _CoroGuard.del().

@iselind
Copy link
Author

iselind commented Sep 4, 2017

Do you have any ideas how to fix this?

@alekseyl1992
Copy link

Got same warning.
This seems to be caused by this lines in AioHTTPTestCase:

    def tearDown(self):
        self.loop.run_until_complete(self.client.close())
        teardown_test_loop(self.loop)

After first lines execution I got:

DeprecationWarning: ClientSession.close() is a coroutine
  warnings.warn(self._msg, DeprecationWarning)

But it seems, something already changed in master branch:
https://github.com/aio-libs/aiohttp/blob/master/aiohttp/test_utils.py#L396

haven't tested it yet

@alekseyl1992
Copy link

I found the source of the problem:
https://github.com/aio-libs/aiohttp/blob/2.2/aiohttp/test_utils.py#L334
and it's already fixed in master branch:
https://github.com/aio-libs/aiohttp/blob/master/aiohttp/test_utils.py#L323

So a temporary workaround would be:

class FixedTestClient(TestClient):
    @asyncio.coroutine
    def close(self):
        if not self._closed:
            for resp in self._responses:
                resp.close()
            for ws in self._websockets:
                yield from ws.close()
            yield from self._session.close()
            yield from self._server.close()
            self._closed = True

class BaseTestCase(AioHTTPTestCase):
    async def _get_client(self, app):
        """Return a TestClient instance."""
        return FixedTestClient(app, loop=self.loop)

So I guess we should just wait for a new version release.

@lock
Copy link

lock bot commented Oct 28, 2019

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].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
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