fix: auto-restart telegram polling loop on failure#373
Conversation
Reviewer's GuideWraps the Telegram adapter polling in a restartable loop with configurable delay, adds explicit error logging for polling failures, and introduces a termination flag to avoid restarts during normal shutdown. Sequence diagram for the Telegram polling restart loop with error handlingsequenceDiagram
participant Adapter as TelegramPlatformAdapter
participant Updater as TelegramApplicationUpdater
loop polling_restart_loop while not _terminating
Adapter->>Updater: start_polling(error_callback=_on_polling_error)
note right of Updater: Returns queue (awaitable polling loop)
Adapter-->>Updater: await queue
alt polling_request_error
Updater-->>Adapter: _on_polling_error(error)
Adapter->>Adapter: log error
end
alt polling_loop_exits_unexpectedly and not _terminating
Adapter->>Adapter: log warning about unexpected exit
Adapter->>Adapter: asyncio.sleep(_polling_restart_delay)
end
alt polling_crashes_with_exception and not _termininating
Adapter->>Adapter: log crash and retry delay
Adapter->>Adapter: asyncio.sleep(_polling_restart_delay)
end
end
rect rgb(230,230,230)
note over Adapter: terminate() sets _terminating = True
end
Updated class diagram for TelegramPlatformAdapter polling lifecycleclassDiagram
class TelegramPlatformAdapter {
- AsyncIOScheduler scheduler
- bool _terminating
- float _polling_restart_delay
+ __init__(config)
+ run() async
- _on_polling_error(error: Exception) void
+ register_commands() async
+ get_client() ExtBot
+ terminate() async
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- When logging unexpected polling exits and crashes, consider including
exc_info=True(or passing the exception aslogger.exception(...)) so that stack traces are captured for easier diagnosis of persistent failures. - Casting
telegram_polling_restart_delaydirectly tofloatmay raiseValueErrorif misconfigured; consider validating or safely defaulting (e.g., catching the error and falling back to the default delay) to avoid the adapter failing at startup due to a bad config value.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- When logging unexpected polling exits and crashes, consider including `exc_info=True` (or passing the exception as `logger.exception(...)`) so that stack traces are captured for easier diagnosis of persistent failures.
- Casting `telegram_polling_restart_delay` directly to `float` may raise `ValueError` if misconfigured; consider validating or safely defaulting (e.g., catching the error and falling back to the default delay) to avoid the adapter failing at startup due to a bad config value.
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/telegram/tg_adapter.py" line_range="95-99" />
<code_context>
self.scheduler = AsyncIOScheduler()
+ self._terminating = False
+ self._polling_restart_delay = float(
+ self.config.get("telegram_polling_restart_delay", 5.0)
+ )
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider validating the polling restart delay from config to avoid zero/negative or non-numeric values.
Casting directly to `float` assumes the config value is valid and positive. Please guard against missing/invalid values (`ValueError`/`TypeError`) and enforce a sensible minimum (e.g. `max(0.1, float(...))`) so misconfiguration can’t cause a tight loop or startup crash.
```suggestion
self.scheduler = AsyncIOScheduler()
self._terminating = False
raw_delay = self.config.get("telegram_polling_restart_delay", 5.0)
try:
delay = float(raw_delay)
except (TypeError, ValueError):
logger.warning(
"Invalid 'telegram_polling_restart_delay' value %r in config, "
"falling back to default 5.0s",
raw_delay,
)
delay = 5.0
if delay < 0.1:
logger.warning(
"Configured 'telegram_polling_restart_delay' (%s) is too small; "
"enforcing minimum of 0.1s to avoid tight restart loops",
delay,
)
delay = 0.1
self._polling_restart_delay = delay
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 27b6499248
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| error_callback=self._on_polling_error | ||
| ) | ||
| logger.info("Telegram Platform Adapter is running.") | ||
| await queue |
There was a problem hiding this comment.
Treat start_polling result as a lifecycle signal
Updater.start_polling in the pinned python-telegram-bot series is an async method that returns an asyncio.Queue once polling has started, so await queue here does not wait for polling to stop. With the new while loop, a healthy startup immediately falls through to the retry path, logs an unexpected-exit warning, then re-enters start_polling and repeatedly hits "already running" errors every retry interval. This creates a persistent error loop and prevents the restart logic from reflecting real polling failures.
Useful? React with 👍 / 👎.
|
@codex address the above comments |
Summary
Testing
|
* fix: auto-restart telegram polling loop on failure (#373) * fix: auto-restart telegram polling loop on failure * fix: harden telegram polling restart lifecycle * fix(telegram): 根据建议优化轮询鲁棒性并处理 Token 失效错误 * fix: 补全配置元数据及 i18n
Motivation
Description
TelegramPlatformAdapter.run()that restartsstart_polling()after unexpected exits and waitstelegram_polling_restart_delayseconds before retrying.error_callback(_on_polling_error) to surface polling request errors into logs for easier diagnosis of proxy/network/API failures._terminatingto avoid restart attempts during normal shutdown and set it interminate()to stop the retry loop.telegram_polling_restart_delay(default5.0), and keep existing behavior for command registration and media-group handling unchanged.Testing
ruff format astrbot/core/platform/sources/telegram/tg_adapter.pywhich completed successfully.ruff check astrbot/core/platform/sources/telegram/tg_adapter.pywhich passed with no issues.python -m py_compile astrbot/core/platform/sources/telegram/tg_adapter.pywhich succeeded with no syntax errors.Codex Task
Summary by Sourcery
Add self-healing behavior to the Telegram platform adapter polling loop so it automatically restarts after unexpected failures while allowing clean shutdowns.
Bug Fixes:
Enhancements: