def _set_custom_task_factory(loop: asyncio.AbstractEventLoop):
    """Wrap task creation to track pending and running tasks.
    When tasks are created, asyncio only maintains a weak reference to them.
    Hence, the garbage collector might destroy the task mid-execution.
    To avoid this, we store a strong reference for the task until it completes.

    Without this, a lot of APIs are basically Heisenbug-generators... e.g.:
    - "asyncio.create_task"
    - "loop.create_task"
    - "asyncio.ensure_future"
    - "asyncio.run_coroutine_threadsafe"

    related:
        - https://bugs.python.org/issue44665
        - https://github.com/python/cpython/issues/88831
        - https://github.com/python/cpython/issues/91887
        - https://textual.textualize.io/blog/2023/02/11/the-heisenbug-lurking-in-your-async-code/
        - https://github.com/python/cpython/issues/91887#issuecomment-1434816045
        - "Task was destroyed but it is pending!"
    """

    platform_task_factory = loop.get_task_factory()

    def factory(loop_, coro, **kwargs):
        if platform_task_factory is not None:
            task = platform_task_factory(loop_, coro, **kwargs)
        else:
            task = asyncio.Task(coro, loop=loop_, **kwargs)
        _running_asyncio_tasks.add(task)
        task.add_done_callback(_running_asyncio_tasks.discard)
        return task

    loop.set_task_factory(factory)
Wrap task creation to track pending and running tasks.
    When tasks are created, asyncio only maintains a weak reference to them.
    Hence, the garbage collector might destroy the task mid-execution.
    To avoid this, we store a strong reference for the task until it completes.

    Without this, a lot of APIs are basically Heisenbug-generators... e.g.:
    - "asyncio.create_task"
    - "loop.create_task"
    - "asyncio.ensure_future"
    - "asyncio.run_coroutine_threadsafe"

    related:
        - https://bugs.python.org/issue44665
        - https://github.com/python/cpython/issues/88831
        - https://github.com/python/cpython/issues/91887
        - https://textual.textualize.io/blog/2023/02/11/the-heisenbug-lurking-in-your-async-code/
        - https://github.com/python/cpython/issues/91887#issuecomment-1434816045
        - "Task was destroyed but it is pending!"
    
electrum/util.py