ref: Use better task cancellation logic (#5397)

Use better task cancellation logic

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
This commit is contained in:
Christophe Bornet 2024-12-28 05:54:55 +01:00 • committed by GitHub
commit 3454ede5a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,7 +3,6 @@ from __future__ import annotations
import asyncio
import os
import platform
import sys
from datetime import datetime, timezone
from typing import TYPE_CHECKING
@ -135,15 +134,11 @@ class TelemetryService(Service):
async def _cancel_task(self, task: asyncio.Task, cancel_msg: str) -> None:
task.cancel(cancel_msg)
try:
await task
except asyncio.CancelledError:
current_task = asyncio.current_task()
if sys.version_info >= (3, 11):
if current_task and current_task.cancelling() > 0:
raise
elif current_task and hasattr(current_task, "_must_cancel") and current_task._must_cancel:
raise
await asyncio.wait([task])
if not task.cancelled():
exc = task.exception()
if exc is not None:
raise exc
async def stop(self) -> None:
if self.do_not_track or self._stopping: