silence asyncio warning on Windows

This commit is contained in:
Aran-Fey
2024-04-11 22:56:13 +02:00
parent 18baa91b5f
commit 9110787104
3 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
# This module silences the following warning:
#
# Task exception was never retrieved
#
# future: <Task finished name='Task-58'
# coro=<IocpProactor.accept.<locals>.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)

View File

@@ -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,
)