Skip to content

The agent that grows with you

fix(telegram): widen cancellation-shielded stop to sibling paths

kshitijk4poor3841910cee79
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index fa4054b1ba5..be6d38159f4 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -3000,10 +3000,8 @@ class TelegramAdapter(BasePlatformAdapter):
"an Updater whose lifecycle lock may still be held."
)
logger.error(
- "[%s] updater.stop() timed out during network-error "
- "reconnect (likely CLOSE-WAIT socket); escalating to fresh-"
- "adapter recovery",
- self.name,
+ "[%s] %s (likely CLOSE-WAIT socket)",
+ self.name, message,
)
self._set_fatal_error(
"telegram_network_error", message, retryable=True
@@ -3475,19 +3473,34 @@ class TelegramAdapter(BasePlatformAdapter):
)
# Stop the local updater cleanly before sleeping. If it's already
# stopped (e.g. PTB raised before updater.running was set) this is
- # a no-op. Bounded with a timeout for the same reason as the
- # network-error path: a CLOSE-WAIT socket can wedge stop() on epoll
- # forever, which would stall the conflict-retry ladder.
+ # a no-op. Bounded with a wall-clock deadline for the same reason
+ # as the network-error path: a CLOSE-WAIT socket can wedge stop()
+ # on epoll forever. Using _await_with_thread_deadline (not
+ # asyncio.wait_for) because PTB/AnyIO cleanup can be cancellation-
+ # shielded — wait_for would hang forever waiting for cancellation
+ # to finish, blocking the conflict-retry ladder.
try:
if self._app and self._app.updater and self._app.updater.running:
try:
- await asyncio.wait_for(self._app.updater.stop(), timeout=_UPDATER_STOP_TIMEOUT)
+ await _await_with_thread_deadline(
+ self._app.updater.stop(), timeout=_UPDATER_STOP_TIMEOUT
+ )
except asyncio.TimeoutError:
- logger.warning(
- "[%s] updater.stop() timed out during conflict "
- "retry (likely CLOSE-WAIT socket); continuing",
- self.name,
+ message = (
+ "Telegram updater.stop() did not finish before the "
+ "conflict-retry deadline; rebuilding the adapter "
+ "instead of reusing an Updater whose lifecycle lock "
+ "may still be held."
+ )
+ logger.error(
+ "[%s] %s (likely CLOSE-WAIT socket)",
+ self.name, message,
)
+ self._set_fatal_error(
+ "telegram_network_error", message, retryable=True
+ )
+ await self._handoff_polling_fatal_error()
+ return
except Exception:
pass
@@ -3595,7 +3608,9 @@ class TelegramAdapter(BasePlatformAdapter):
self._set_fatal_error("telegram_polling_conflict", message, retryable=False)
try:
if self._app and self._app.updater:
- await asyncio.wait_for(self._app.updater.stop(), timeout=_UPDATER_STOP_TIMEOUT)
+ await _await_with_thread_deadline(
+ self._app.updater.stop(), timeout=_UPDATER_STOP_TIMEOUT
+ )
except asyncio.TimeoutError:
logger.warning(
"[%s] updater.stop() timed out after exhausting conflict "