fix: make asyncio cancellation handling compatible with 3.10 (#4303)

Fix asyncio cancellation handling for Python 3.11 compatibility

Co-authored-by: Christophe Bornet <cbornet@hotmail.com>
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-10-28 18:56:30 -03:00 committed by GitHub
commit 924e02f46d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import os
import platform
import sys
from datetime import datetime, timezone
from typing import TYPE_CHECKING
@ -136,7 +137,10 @@ class TelemetryService(Service):
await task
except asyncio.CancelledError:
current_task = asyncio.current_task()
if current_task and current_task.cancelling() > 0:
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
async def stop(self) -> None: