fix(telegram): final message waits for the draft push in flight

This commit is contained in:
hh
2026-08-28 18:32:45 +02:00
parent 3995741cd5
commit c711dcab13
2 changed files with 18 additions and 1 deletions
@@ -52,6 +52,7 @@ class Draft:
self._broken = False
self._last_sent = 0.0
self._task: asyncio.Task[None] | None = None
self._inflight: asyncio.Future[None] | None = None
def start(self) -> None:
if self._task is None:
@@ -68,17 +69,26 @@ class Draft:
self._dirty = True
async def stop(self) -> None:
"""Stop pushing and wait for the push in flight.
The final ``sendMessage`` must reach Telegram after the last draft,
or the draft lands on top of the message and lingers for its 30 s.
"""
if self._task is None:
return
self._task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await self._task
self._task = None
if self._inflight is not None:
with contextlib.suppress(Exception):
await self._inflight
async def _run(self) -> None:
while not self._broken:
if self._dirty or time.monotonic() - self._last_sent > _KEEPALIVE:
await self._push()
self._inflight = asyncio.ensure_future(self._push())
await asyncio.shield(self._inflight)
await asyncio.sleep(self._interval)
async def _push(self) -> None: