From 266aa3e0fe765d95a3e9679c11824e197f67a2ae Mon Sep 17 00:00:00 2001 From: monosans Date: Fri, 6 Oct 2023 10:45:50 +0300 Subject: [PATCH 1/2] Fix dangling asyncio tasks --- api/responder.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/responder.py b/api/responder.py index d235cc4..79431d5 100644 --- a/api/responder.py +++ b/api/responder.py @@ -7,6 +7,7 @@ import aiohttp import asyncio import starlette +from typing import Any, Coroutine, Set from rich import print from dotenv import load_dotenv @@ -23,6 +24,15 @@ CRITICAL_API_ERRORS = ['invalid_api_key', 'account_deactivated'] keymanager = providerkeys.manager +background_tasks: Set[asyncio.Task[Any]] = set() + + +def create_background_task(coro: Coroutine[Any, Any, Any]) -> None: + task = asyncio.create_task(coro) + background_tasks.add(task) + task.add_done_callback(background_tasks.discard) + + async def respond( path: str='/v1/chat/completions', user: dict=None, @@ -166,7 +176,7 @@ async def respond( if (not is_stream) and server_json_response: yield json.dumps(server_json_response) - asyncio.create_task( + create_background_task( after_request.after_request( incoming_request=incoming_request, target_request=target_request, From 0b08c5b7127828c533153710bbeddf34f82d3b2f Mon Sep 17 00:00:00 2001 From: monosans Date: Sat, 7 Oct 2023 18:07:35 +0300 Subject: [PATCH 2/2] Add docstring to create_background_task --- api/responder.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/responder.py b/api/responder.py index 79431d5..7ad74fa 100644 --- a/api/responder.py +++ b/api/responder.py @@ -28,6 +28,10 @@ background_tasks: Set[asyncio.Task[Any]] = set() def create_background_task(coro: Coroutine[Any, Any, Any]) -> None: + """asyncio.create_task, which prevents the task from being garbage collected. + + https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task + """ task = asyncio.create_task(coro) background_tasks.add(task) task.add_done_callback(background_tasks.discard)