Merge pull request #1389 from NousResearch/hermes/hermes-7ef7cb6a

fix(telegram): check updater/app state before disconnect
This commit is contained in:
Teknium 2026-03-14 21:53:54 -07:00 committed by GitHub
commit 69045711c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 2 deletions

View file

@ -275,8 +275,11 @@ class TelegramAdapter(BasePlatformAdapter):
if self._app:
try:
await self._app.updater.stop()
await self._app.stop()
# Only stop the updater if it's running
if self._app.updater and self._app.updater.running:
await self._app.updater.stop()
if self._app.running:
await self._app.stop()
await self._app.shutdown()
except Exception as e:
logger.warning("[%s] Error during Telegram disconnect: %s", self.name, e, exc_info=True)

View file

@ -98,3 +98,27 @@ async def test_polling_conflict_stops_polling_and_notifies_handler(monkeypatch):
assert adapter.has_fatal_error is True
updater.stop.assert_awaited()
fatal_handler.assert_awaited_once()
@pytest.mark.asyncio
async def test_disconnect_skips_inactive_updater_and_app(monkeypatch):
adapter = TelegramAdapter(PlatformConfig(enabled=True, token="***"))
updater = SimpleNamespace(running=False, stop=AsyncMock())
app = SimpleNamespace(
updater=updater,
running=False,
stop=AsyncMock(),
shutdown=AsyncMock(),
)
adapter._app = app
warning = MagicMock()
monkeypatch.setattr("gateway.platforms.telegram.logger.warning", warning)
await adapter.disconnect()
updater.stop.assert_not_awaited()
app.stop.assert_not_awaited()
app.shutdown.assert_awaited_once()
warning.assert_not_called()