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 asyncio
import os import os
import platform import platform
import sys
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -136,8 +137,11 @@ class TelemetryService(Service):
await task await task
except asyncio.CancelledError: except asyncio.CancelledError:
current_task = asyncio.current_task() current_task = asyncio.current_task()
if sys.version_info >= (3, 11):
if current_task and current_task.cancelling() > 0: if current_task and current_task.cancelling() > 0:
raise raise
elif current_task and hasattr(current_task, "_must_cancel") and current_task._must_cancel:
raise
async def stop(self) -> None: async def stop(self) -> None:
if self.do_not_track or self._stopping: if self.do_not_track or self._stopping: