From 9110787104147de06c1655be026801674c42ae0f Mon Sep 17 00:00:00 2001 From: Aran-Fey Date: Thu, 11 Apr 2024 22:56:13 +0200 Subject: [PATCH] silence asyncio warning on Windows --- .../IocpProactor_accept_locals_accept_coro.py | 48 +++++++++++++++++++ ...BasePipeTransport_call_connection_lost.py} | 0 rio/patches_for_3rd_party_stuff/__init__.py | 6 +++ 3 files changed, 54 insertions(+) create mode 100644 rio/patches_for_3rd_party_stuff/IocpProactor_accept_locals_accept_coro.py rename rio/{patches_for_3rd_party_stuff.py => patches_for_3rd_party_stuff/ProactorBasePipeTransport_call_connection_lost.py} (100%) create mode 100644 rio/patches_for_3rd_party_stuff/__init__.py diff --git a/rio/patches_for_3rd_party_stuff/IocpProactor_accept_locals_accept_coro.py b/rio/patches_for_3rd_party_stuff/IocpProactor_accept_locals_accept_coro.py new file mode 100644 index 00000000..c0ec0b38 --- /dev/null +++ b/rio/patches_for_3rd_party_stuff/IocpProactor_accept_locals_accept_coro.py @@ -0,0 +1,48 @@ +# This module silences the following warning: +# +# Task exception was never retrieved +# +# future: .accept_coro() done, defined at +# C:\Python312\Lib\asyncio\windows_events.py:558> exception=OSError(22, 'The I/O +# operation has been aborted because of either a thread exit or an application +# request', None, 995, None)> Traceback (most recent call last): File +# "C:\Python312\Lib\asyncio\windows_events.py", line 561, in accept_coro await +# future OSError: [WinError 995] The I/O operation has been aborted because of +# either a thread exit or an application request + +import asyncio.windows_events + +import introspection + + +def query_task_exception(task: asyncio.Task) -> None: + try: + task.exception() + except: + pass + + +# Since the offending function (`accept_coro`) is a nested function, the only +# solution I can think of is to monkeypatch `tasks.ensure_future` +tasks_module = asyncio.windows_events.tasks # type: ignore + + +class tasks: + @staticmethod + def ensure_future(*args, **kwargs): + task = asyncio.ensure_future(*args, **kwargs) + task.add_done_callback(query_task_exception) + return task + + +def accept(original_method, *args, **kwargs): + asyncio.windows_events.tasks = tasks # type: ignore + + try: + return original_method(*args, **kwargs) + finally: + asyncio.windows_events.tasks = tasks_module # type: ignore + + +introspection.wrap_method(asyncio.windows_events.IocpProactor, accept) diff --git a/rio/patches_for_3rd_party_stuff.py b/rio/patches_for_3rd_party_stuff/ProactorBasePipeTransport_call_connection_lost.py similarity index 100% rename from rio/patches_for_3rd_party_stuff.py rename to rio/patches_for_3rd_party_stuff/ProactorBasePipeTransport_call_connection_lost.py diff --git a/rio/patches_for_3rd_party_stuff/__init__.py b/rio/patches_for_3rd_party_stuff/__init__.py new file mode 100644 index 00000000..8884dd92 --- /dev/null +++ b/rio/patches_for_3rd_party_stuff/__init__.py @@ -0,0 +1,6 @@ +from . import ( + IocpProactor_accept_locals_accept_coro as IocpProactor_accept_locals_accept_coro, +) +from . import ( + ProactorBasePipeTransport_call_connection_lost as ProactorBasePipeTransport_call_connection_lost, +)