File size: 784 Bytes
ca766b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from __future__ import annotations

import sys
from typing import Any


_HOOK_INSTALLED = False


def install_asyncio_cleanup_hook() -> None:
    global _HOOK_INSTALLED
    if _HOOK_INSTALLED:
        return
    previous_hook = sys.unraisablehook

    def hook(args: Any) -> None:
        if _is_asyncio_invalid_fd_cleanup(args):
            return
        previous_hook(args)

    sys.unraisablehook = hook
    _HOOK_INSTALLED = True


def _is_asyncio_invalid_fd_cleanup(args: Any) -> bool:
    if getattr(args, "exc_type", None) is not ValueError:
        return False
    if str(getattr(args, "exc_value", "")) != "Invalid file descriptor: -1":
        return False
    owner = getattr(args, "object", None)
    return getattr(owner, "__qualname__", "") == "BaseEventLoop.__del__"