ops bot: timeouts on Telegram polling and a watchdog that restarts a stuck poll
On 12.10 one ConnectError through the proxy left getUpdates hanging on a dead (CLOSE-WAIT) connection for 1.5 days: reminders still went out, incoming messages got no answer. Explicit connect/read/pool timeouts for both request pools, long-poll timeout 25 s, and a watchdog: no successful getUpdates for 3 minutes → exit, systemd starts a fresh process. Tested on the VM by stopping the proxy: restart after 185 s, polling back once the proxy returned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+38
-4
@@ -15,6 +15,7 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
||||
from telegram.constants import ParseMode
|
||||
from telegram.ext import (Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler,
|
||||
filters)
|
||||
from telegram.request import HTTPXRequest
|
||||
|
||||
from . import brain, fmt, voice
|
||||
from .core import Ops, OpsError, ru_day
|
||||
@@ -334,6 +335,35 @@ def make_api() -> web.Application:
|
||||
return app
|
||||
|
||||
|
||||
# MARK: - Polling watchdog
|
||||
#
|
||||
# 12.10.2026: one ConnectError through the proxy left getUpdates hanging on a dead
|
||||
# connection (CLOSE-WAIT) for 1.5 days — reminders still went out, incoming messages
|
||||
# didn't. Every successful getUpdates is stamped; when none succeeds for 3 minutes the
|
||||
# process exits and systemd starts a fresh one.
|
||||
|
||||
last_poll_ok = [0.0]
|
||||
|
||||
|
||||
class PollRequest(HTTPXRequest):
|
||||
async def do_request(self, url, method, *args, **kwargs):
|
||||
result = await super().do_request(url, method, *args, **kwargs)
|
||||
if url.endswith("/getUpdates") and result[0] == 200:
|
||||
last_poll_ok[0] = asyncio.get_running_loop().time()
|
||||
return result
|
||||
|
||||
|
||||
async def poll_watchdog():
|
||||
loop = asyncio.get_running_loop()
|
||||
last_poll_ok[0] = loop.time()
|
||||
while True:
|
||||
await asyncio.sleep(30)
|
||||
stale = loop.time() - last_poll_ok[0]
|
||||
if stale > 180:
|
||||
log.error("no successful getUpdates for %.0f s — restarting", stale)
|
||||
os._exit(1)
|
||||
|
||||
|
||||
# MARK: - Main
|
||||
|
||||
async def main():
|
||||
@@ -349,9 +379,12 @@ async def main():
|
||||
if not CFG["token"]:
|
||||
log.warning("TELEGRAM_TOKEN not set: API only")
|
||||
await asyncio.Event().wait()
|
||||
builder = Application.builder().token(CFG["token"])
|
||||
if CFG["proxy"]:
|
||||
builder = builder.proxy(CFG["proxy"]).get_updates_proxy(CFG["proxy"])
|
||||
proxy = CFG["proxy"] or None
|
||||
builder = (Application.builder().token(CFG["token"])
|
||||
.request(HTTPXRequest(proxy=proxy, connect_timeout=10, read_timeout=20, write_timeout=20,
|
||||
pool_timeout=10, connection_pool_size=8))
|
||||
.get_updates_request(PollRequest(proxy=proxy, connect_timeout=10, read_timeout=40,
|
||||
write_timeout=10, pool_timeout=10)))
|
||||
tg = builder.build()
|
||||
async def on_error(update, ctx):
|
||||
log.error("handler failed", exc_info=ctx.error)
|
||||
@@ -367,8 +400,9 @@ async def main():
|
||||
tg.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, on_text))
|
||||
async with tg:
|
||||
await tg.start()
|
||||
await tg.updater.start_polling(drop_pending_updates=False)
|
||||
await tg.updater.start_polling(drop_pending_updates=False, timeout=25)
|
||||
asyncio.create_task(reminder_loop(tg.bot))
|
||||
asyncio.create_task(poll_watchdog())
|
||||
await asyncio.Event().wait()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user