Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,23 @@ async def _handle_adapter_fatal_error(self, adapter: BasePlatformAdapter) -> Non
If the error is retryable (e.g. network blip, DNS failure), queue the
platform for background reconnection instead of giving up permanently.
"""
# Snapshot the current owner of this platform slot before doing
# anything else. If it's neither this adapter nor empty, a different
# adapter has already taken over (e.g. this is a delayed notification
# from a background retry chain that raced with, and lost to, a
# reconnect that already succeeded). Acting on a stale notification
# would overwrite an already-healthy platform's runtime status and
# incorrectly re-queue it for reconnection, so bail out before any of
# that happens.
existing = self.adapters.get(adapter.platform)
if existing is not None and existing is not adapter:
logger.debug(
"Ignoring stale fatal error from a superseded %s adapter instance: %s",
adapter.platform.value,
adapter.fatal_error_code or "unknown",
)
return

logger.error(
"Fatal %s adapter error (%s): %s",
adapter.platform.value,
Expand All @@ -3677,13 +3694,15 @@ async def _handle_adapter_fatal_error(self, adapter: BasePlatformAdapter) -> Non
error_message=adapter.fatal_error_message,
)

existing = self.adapters.get(adapter.platform)
if existing is adapter:
try:
await adapter.disconnect()
finally:
self.adapters.pop(adapter.platform, None)
self.delivery_router.adapters = self.adapters
# Claim this adapter for teardown before awaiting disconnect() —
# a second fatal-error notification for the same adapter (e.g.
# from a concurrent recovery path) would otherwise still see
# itself as "existing" during the await below and disconnect()
# the same object twice.
self.adapters.pop(adapter.platform, None)
self.delivery_router.adapters = self.adapters
await adapter.disconnect()

# Queue retryable failures for background reconnection
if adapter.fatal_error_retryable:
Expand Down Expand Up @@ -9743,7 +9762,7 @@ async def _prepare_inbound_message_text(
if "@" in message_text:
try:
from agent.context_references import preprocess_context_references_async
from agent.model_metadata import get_model_context_length_async
from agent.model_metadata import get_model_context_length

_msg_cwd = os.environ.get("TERMINAL_CWD", os.path.expanduser("~"))
_msg_runtime = _resolve_runtime_agent_kwargs()
Expand All @@ -9757,7 +9776,7 @@ async def _prepare_inbound_message_text(
_msg_config_ctx = int(_msg_raw_ctx)
except Exception:
pass
_msg_ctx_len = await get_model_context_length_async(
_msg_ctx_len = get_model_context_length(
self._model,
base_url=self._base_url or _msg_runtime.get("base_url") or "",
api_key=_msg_runtime.get("api_key") or "",
Expand Down Expand Up @@ -10095,7 +10114,7 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str, run_g
if history and len(history) >= 4:
from agent.model_metadata import (
estimate_messages_tokens_rough,
get_model_context_length_async,
get_model_context_length,
)

# Read model + compression config from config.yaml.
Expand Down Expand Up @@ -10196,7 +10215,7 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str, run_g
pass

if _hyg_compression_enabled:
_hyg_context_length = await get_model_context_length_async(
_hyg_context_length = get_model_context_length(
_hyg_model,
base_url=_hyg_base_url or "",
api_key=_hyg_api_key or "",
Expand Down
11 changes: 1 addition & 10 deletions plugins/platforms/telegram/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,17 +2165,8 @@ async def _handle_polling_conflict(self, error: Exception) -> None:
await asyncio.sleep(RETRY_DELAY)
await self._drain_polling_connections()

# Capture a stable local reference: self._app can be reassigned to
# None by a concurrent disconnect() while we're suspended across
# the awaits above (same race #55992 fixed on the network path).
# Re-reading self._app after that point would raise
# AttributeError deep inside start_polling instead of failing fast
# here, where the except below reschedules or escalates to fatal.
app = self._app
try:
if not app:
raise RuntimeError("Telegram application was torn down during conflict reconnect")
await app.updater.start_polling(
await self._app.updater.start_polling(
allowed_updates=Update.ALL_TYPES,
drop_pending_updates=False,
error_callback=self._polling_error_callback_ref,
Comment on lines +2169 to 2172

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Removed local-reference snapshot reintroduces race condition in Telegram polling conflict recovery (bug)

In _handle_polling_conflict at plugins/platforms/telegram/adapter.py, the patch removed the local variable snapshot app = self._app that was captured before two await points (lines 2165-2166: asyncio.sleep(RETRY_DELAY) and await self._drain_polling_connections()). The removed code comments explicitly documented that self._app can be reassigned to None by a concurrent disconnect() during those awaits, citing issue NousResearch#55992.

After the patch, line 2169 reads self._app directly: await self._app.updater.start_polling(...). If disconnect() (line 3042: self._app = None) runs concurrently during the await suspension — e.g., from a gateway shutdown calling stop() → _bounded_adapter_teardown → disconnect() — accessing self._app.updater raises AttributeError: 'NoneType' object has no attribute 'updater'.

The sibling method _handle_polling_network_error (line 1780) still preserves the exact same capture pattern with an explicit comment about the concurrent disconnect() race, confirming the pattern was deliberately applied to protect async re-entrancy in PTB error callbacks.

Impact is bounded because the except Exception handler at line 2180 catches both the old RuntimeError and the new AttributeError, rescheduling a retry. The regression is primarily in diagnostic quality (unclear error message vs. explicit 'Telegram application was torn down') and the reintroduction of a known, previously-fixed race pattern.

💡 Suggestion: Restore the local-reference snapshot pattern that was removed: before the try block at line 2168, capture app = self._app. Then inside the try, guard with if not app: raise RuntimeError('Telegram application was torn down during conflict reconnect') and use app.updater.start_polling(...) instead of self._app.updater.start_polling(...). This matches the proven pattern still present in the sibling method _handle_polling_network_error at lines 1780-1793.

📋 Prompt for AI Agents

In plugins/platforms/telegram/adapter.py, restore the concurrency guard in _handle_polling_conflict:

  1. Before line 2168 (try:), add: app = self._app
  2. At line 2168-2169, replace try:\n await self._app.updater.start_polling( with:
try:
    if not app:
        raise RuntimeError("Telegram application was torn down during conflict reconnect")
    await app.updater.start_polling(

This restores the local-reference snapshot pattern that protects against self._app being set to None by a concurrent disconnect() during the await points above. The sibling method _handle_polling_network_error at line 1780 still uses this pattern with the same documented rationale.

Expand Down
Loading