From 1f63113d8d418a8101cea925625f6d1d0cef5cf7 Mon Sep 17 00:00:00 2001 From: Goldy <153996346+g0ldyy@users.noreply.github.com> Date: Fri, 5 Jul 2024 01:36:57 +0200 Subject: [PATCH] fix: try to fix session unclosed issue with debrid stream proxy --- comet/api/stream.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/comet/api/stream.py b/comet/api/stream.py index 3654ed8..a7cc4a0 100644 --- a/comet/api/stream.py +++ b/comet/api/stream.py @@ -296,14 +296,14 @@ async def playback(request: Request, b64config: str, hash: str, index: str): async def stream_content(headers: dict): async with aiohttp.ClientSession() as session: - response = await session.get(download_link, headers=headers) - while True: - chunk = await response.content.read( - settings.PROXY_DEBRID_STREAM_BYTES_PER_CHUNK - ) # 10 MB chunks - if not chunk: - break - yield chunk + async with session.get(download_link, headers=headers) as response: + while True: + chunk = await response.content.read( + settings.PROXY_DEBRID_STREAM_BYTES_PER_CHUNK + ) # 10 MB chunks + if not chunk: + break + yield chunk range = None range_header = request.headers.get("range") @@ -314,19 +314,19 @@ async def stream_content(headers: dict): end = int(end) if end else "" range = f"bytes={start}-{end}" - response = await session.get( - download_link, headers={"Range": f"bytes={start}-{end}"} - ) - if response.status == 206: - return StreamingResponse( - stream_content({"Range": range}), - status_code=206, - headers={ - "Content-Range": response.headers["Content-Range"], - "Content-Length": response.headers["Content-Length"], - "Accept-Ranges": "bytes", - }, - ) + async with await session.get(download_link, headers={"Range": f"bytes={start}-{end}"}) as response: + await session.close() + + if response.status == 206: + return StreamingResponse( + stream_content({"Range": range}), + status_code=206, + headers={ + "Content-Range": response.headers["Content-Range"], + "Content-Length": response.headers["Content-Length"], + "Accept-Ranges": "bytes", + }, + ) return