diff --git a/skyvern/forge/sdk/core/aiohttp_helper.py b/skyvern/forge/sdk/core/aiohttp_helper.py index 1ce597e69b..2d9e78dee8 100644 --- a/skyvern/forge/sdk/core/aiohttp_helper.py +++ b/skyvern/forge/sdk/core/aiohttp_helper.py @@ -68,7 +68,11 @@ async def aiohttp_get_json( raise_exception: bool = True, retry_timeout: float = 0, ) -> dict[str, Any]: - async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=timeout)) as session: + own_session = session is None + if own_session: + session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=timeout)) + + try: count = 0 while count <= retry: try: @@ -90,6 +94,9 @@ async def aiohttp_get_json( await asyncio.sleep(retry_timeout) count += 1 raise Exception(f"Failed to fetch data from {url}") + finally: + if own_session: + await session.close() async def aiohttp_get_text( @@ -139,7 +146,11 @@ async def aiohttp_post( raise_exception: bool = True, retry_timeout: float = 0, ) -> dict[str, Any] | None: - async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=timeout)) as session: + own_session = session is None + if own_session: + session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=timeout)) + + try: count = 0 while count <= retry: try: @@ -170,6 +181,9 @@ async def aiohttp_post( await asyncio.sleep(retry_timeout) count += 1 raise Exception(f"Failed post request url={url}") + finally: + if own_session: + await session.close() async def aiohttp_delete(