Skip to content

Commit

Permalink
Add warning for creation a session outside of coroutine (#1468)
Browse files Browse the repository at this point in the history
* Add warning for creation a session outside of coroutine

* Call exception handler also

* Update CHANGES
  • Loading branch information
asvetlov authored Dec 9, 2016
1 parent b89a462 commit c4539a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

-

Expand Down
11 changes: 11 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ 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)
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)
self._cookie_jar = cookie_jar
Expand Down
6 changes: 6 additions & 0 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
sess.close()

0 comments on commit c4539a7

Please sign in to comment.