Slightly redesign session close logic
This commit is contained in:
1 parent
0431a2d98e
commit
90fb6ea9a7
1 file changed
+6
-4
@@ -74,8 +74,10 @@ class Twitch:
|
|||||||
self._mnt_task: asyncio.Task[None] | None = None
|
self._mnt_task: asyncio.Task[None] | None = None
|
||||||
|
|
||||||
async def get_session(self) -> aiohttp.ClientSession:
|
async def get_session(self) -> aiohttp.ClientSession:
|
||||||
if self._session is not None:
|
if (session := self._session) is not None:
|
||||||
return self._session
|
if session.closed:
|
||||||
|
raise RuntimeError("Session is closed")
|
||||||
|
return session
|
||||||
cookie_jar = aiohttp.CookieJar()
|
cookie_jar = aiohttp.CookieJar()
|
||||||
if COOKIES_PATH.exists():
|
if COOKIES_PATH.exists():
|
||||||
cookie_jar.load(COOKIES_PATH)
|
cookie_jar.load(COOKIES_PATH)
|
||||||
@@ -100,8 +102,8 @@ class Twitch:
|
|||||||
if self._session is not None:
|
if self._session is not None:
|
||||||
cookie_jar = cast(aiohttp.CookieJar, self._session.cookie_jar)
|
cookie_jar = cast(aiohttp.CookieJar, self._session.cookie_jar)
|
||||||
cookie_jar.save(COOKIES_PATH)
|
cookie_jar.save(COOKIES_PATH)
|
||||||
session, self._session = self._session, None
|
await self._session.close()
|
||||||
await session.close()
|
self._session = None
|
||||||
await self.websocket.stop()
|
await self.websocket.stop()
|
||||||
# wait at least half a second + whatever it takes to complete the closing
|
# wait at least half a second + whatever it takes to complete the closing
|
||||||
# this allows aiohttp to safely close the session
|
# this allows aiohttp to safely close the session
|
||||||
|
|||||||
Reference in new issue
Block a user