build-small-hackathon/hackathon-advisor
16
1from __future__ import annotations2 3import sys4from typing import Any5 6 7_HOOK_INSTALLED = False8 9 10def install_asyncio_cleanup_hook() -> None:11 global _HOOK_INSTALLED12 if _HOOK_INSTALLED:13 return14 previous_hook = sys.unraisablehook15 16 def hook(args: Any) -> None:17 if _is_asyncio_invalid_fd_cleanup(args):18 return19 previous_hook(args)20 21 sys.unraisablehook = hook22 _HOOK_INSTALLED = True23 24 25def _is_asyncio_invalid_fd_cleanup(args: Any) -> bool:26 if getattr(args, "exc_type", None) is not ValueError:27 return False28 if str(getattr(args, "exc_value", "")) != "Invalid file descriptor: -1":29 return False30 owner = getattr(args, "object", None)31 return getattr(owner, "__qualname__", "") == "BaseEventLoop.__del__"32 