From 30356f9dca9e6e70c26e3fb29c04bd5313b2d772 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Fri, 9 Dec 2016 15:14:37 +0200 Subject: [PATCH 1/3] Add warning for creation a session outside of coroutine --- aiohttp/client.py | 5 +++++ tests/test_client_session.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/aiohttp/client.py b/aiohttp/client.py index 6c5d02944dc..f7c2cf3e21e 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -54,6 +54,11 @@ def __init__(self, *, connector=None, loop=None, cookies=None, if loop.get_debug(): self._source_traceback = traceback.extract_stack(sys._getframe(1)) + if not loop.is_running(): + warnings.warn("Creating a client session outside of coroutine is " + "a very dangerous idea", ResourceWarning, + stacklevel=2) + if cookie_jar is None: cookie_jar = CookieJar(loop=loop) self._cookie_jar = cookie_jar diff --git a/tests/test_client_session.py b/tests/test_client_session.py index 74e0c07a85b..ecc5f07e966 100644 --- a/tests/test_client_session.py +++ b/tests/test_client_session.py @@ -435,3 +435,9 @@ def test_proxy_str(session, params): allow_redirects=True, proxy='http://proxy.com', **params)] + + +def test_create_session_outside_of_coroutine(loop): + with pytest.warns(ResourceWarning): + sess = ClientSession(loop=loop) + del sess From 6c0c58e24d8b81630bb5380a27cb8af63d689497 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Fri, 9 Dec 2016 17:36:09 +0200 Subject: [PATCH 2/3] Call exception handler also --- aiohttp/client.py | 6 ++++++ tests/test_client_session.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index f7c2cf3e21e..4b498746421 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -58,6 +58,12 @@ def __init__(self, *, connector=None, loop=None, cookies=None, warnings.warn("Creating a client session outside of coroutine is " "a very dangerous idea", ResourceWarning, stacklevel=2) + context = {'client_session': self, + 'message': 'Creating a client session outside ' + 'of coroutine'} + if self._source_traceback is not None: + context['source_traceback'] = self._source_traceback + loop.call_exception_handler(context) if cookie_jar is None: cookie_jar = CookieJar(loop=loop) diff --git a/tests/test_client_session.py b/tests/test_client_session.py index ecc5f07e966..e51fe78f468 100644 --- a/tests/test_client_session.py +++ b/tests/test_client_session.py @@ -440,4 +440,4 @@ def test_proxy_str(session, params): def test_create_session_outside_of_coroutine(loop): with pytest.warns(ResourceWarning): sess = ClientSession(loop=loop) - del sess + sess.close() From 625de7c0a871d04c78314396b9b7a65c1ecea97e Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Fri, 9 Dec 2016 17:37:27 +0200 Subject: [PATCH 3/3] Update CHANGES --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 333a71b3543..e76b53766c6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -45,7 +45,7 @@ CHANGES - Allow to raise web exceptions on router resolving stage #1460 -- +- Add a warning for session creation outside of coroutine #1468 -