fix(telegram): use get_running_loop in polling-conflict retry reschedule (#41716)

The conflict-retry path called asyncio.get_event_loop() to reschedule
itself when a retry's start_polling raised. On Python 3.11+ (our floor)
that raises 'RuntimeError: There is no current event loop in thread
MainThread' when no loop is attached to the thread, which is what
happens when PTB dispatches this error callback. The retry never gets
scheduled, the adapter goes silent-but-alive, and gateway --replace
keeps spawning fresh instances that hit the same wall — the crash loop
reported in #19471 (worse under multi-profile, where two bots hold the
same conflict open).

We are inside a coroutine here, so asyncio.get_running_loop() is the
correct, guaranteed-valid replacement. Only get_event_loop() call in
any platform adapter, so no sibling sites.

Fixes #19471
This commit is contained in:
Teknium
2026-06-07 22:10:03 -07:00
committed by GitHub
parent b5f7a1f299
commit 2e62862784
2 changed files with 96 additions and 1 deletions
+7 -1
View File
@@ -1143,7 +1143,13 @@ class TelegramAdapter(BasePlatformAdapter):
# gateway process is alive and reports "connected" but
# no messages are received or sent.
if self._polling_conflict_count < MAX_CONFLICT_RETRIES:
loop = asyncio.get_event_loop()
# We are inside a running coroutine, so the running loop is
# guaranteed to exist. asyncio.get_event_loop() is deprecated
# and raises "RuntimeError: There is no current event loop in
# thread 'MainThread'" on Python 3.10+ when invoked from a
# context without an attached loop (which can happen when PTB
# dispatches this error callback). Use get_running_loop().
loop = asyncio.get_running_loop()
self._polling_error_task = loop.create_task(
self._handle_polling_conflict(retry_err)
)