diff --git a/aiohttp_requests/__init__.py b/aiohttp_requests/__init__.py index f7859e2..722e72b 100644 --- a/aiohttp_requests/__init__.py +++ b/aiohttp_requests/__init__.py @@ -22,13 +22,21 @@ def __init__(self, *args, **kwargs): @property def session(self): """ An instance of aiohttp.ClientSession """ - if not self._session or self._session.closed: + if not self._session or self._session.closed or self._session.loop.is_closed(): self._session = aiohttp.ClientSession(*self._session_args[0], **self._session_args[1]) return self._session def __getattr__(self, attr): if attr.upper() in aiohttp.hdrs.METH_ALL: - return functools.partial(self.session._request, attr.upper()) + @functools.wraps(self.session._request) + def session_request(*args, **kwargs): + """ + This ensures `self.session` is always called where it can check the session/loop state so can't use functools.partials + as monkeypatch seems to do something weird where __getattr__ is only called once for each attribute after patch is undone + """ + return self.session._request(attr.upper(), *args, **kwargs) + + return session_request else: return super().__getattribute__(attr) diff --git a/tests/test_aiohttp_requests.py b/tests/test_aiohttp_requests.py index 8da7523..139424f 100644 --- a/tests/test_aiohttp_requests.py +++ b/tests/test_aiohttp_requests.py @@ -26,10 +26,12 @@ async def test_aiohttp_requests_integration(): async def test_aiohttp_requests_after_close(loop): + # Closing ourself requests.close() await test_aiohttp_requests_integration() + # Closing aiohttp session await requests.session.close() await test_aiohttp_requests_integration()