|
48 | 48 | import weakref |
49 | 49 | from time import perf_counter_ns |
50 | 50 | from typing import (TYPE_CHECKING, Any, Awaitable, Callable, Dict, Optional, |
51 | | - Set, Tuple) |
| 51 | + Set, Tuple, Union) |
52 | 52 |
|
53 | 53 | from aws_advanced_python_wrapper.aio import topology_store |
54 | 54 | from aws_advanced_python_wrapper.hostinfo import HostRole |
55 | 55 | from aws_advanced_python_wrapper.utils.log import Logger |
56 | 56 | from aws_advanced_python_wrapper.utils.messages import Messages |
57 | 57 |
|
58 | 58 | if TYPE_CHECKING: |
| 59 | + from concurrent.futures import Future |
| 60 | + |
59 | 61 | from aws_advanced_python_wrapper.aio.plugin_service import \ |
60 | 62 | AsyncPluginService |
61 | 63 | from aws_advanced_python_wrapper.aio.topology_store import TopologyKey |
@@ -127,6 +129,7 @@ def __init__( |
127 | 129 | self._probe_host = probe_host |
128 | 130 |
|
129 | 131 | self._task: Optional[asyncio.Task[None]] = None |
| 132 | + self._scheduled: Set[Union[asyncio.Task[None], Future[None]]] = set() |
130 | 133 | self._stop_event = asyncio.Event() |
131 | 134 | self._last_known_writer: Optional[str] = None |
132 | 135 | self._high_refresh_until_ns: int = 0 |
@@ -809,17 +812,21 @@ def _schedule(self, coro: Awaitable[None]) -> bool: |
809 | 812 | return False |
810 | 813 | try: |
811 | 814 | if loop is asyncio.get_running_loop(): |
812 | | - loop.create_task(coro) # type: ignore[arg-type] |
| 815 | + self._keep_until_done(loop.create_task(coro)) # type: ignore[arg-type] |
813 | 816 | return True |
814 | 817 | except RuntimeError: |
815 | 818 | pass |
816 | 819 | try: |
817 | | - asyncio.run_coroutine_threadsafe(coro, loop) # type: ignore[arg-type] |
| 820 | + self._keep_until_done(asyncio.run_coroutine_threadsafe(coro, loop)) # type: ignore[arg-type] |
818 | 821 | return True |
819 | 822 | except RuntimeError: |
820 | 823 | coro.close() # type: ignore[attr-defined] |
821 | 824 | return False |
822 | 825 |
|
| 826 | + def _keep_until_done(self, work: Union[asyncio.Task[None], Future[None]]) -> None: |
| 827 | + self._scheduled.add(work) |
| 828 | + work.add_done_callback(self._scheduled.discard) |
| 829 | + |
823 | 830 | async def _reset(self) -> None: |
824 | 831 | """Drop everything tied to the pre-switchover cluster. |
825 | 832 |
|
|
0 commit comments