From a32ad1a656f0a7d5caeb572db5fdd24fdf58874e Mon Sep 17 00:00:00 2001 From: teknium Date: Wed, 11 Feb 2026 00:05:30 +0000 Subject: [PATCH] Fix infinite interrupt loop in gateway by consuming pending messages with .pop() and clearing interrupt events before recursion - Added logic to clear the adapter's interrupt event to prevent infinite loops during message processing. - Updated the get_pending_message method to pop messages from the pending queue, ensuring proper message handling. --- gateway/platforms/base.py | 2 +- gateway/run.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py index aea73bb09..2e9da3354 100644 --- a/gateway/platforms/base.py +++ b/gateway/platforms/base.py @@ -286,7 +286,7 @@ class BasePlatformAdapter(ABC): def get_pending_message(self, session_key: str) -> Optional[MessageEvent]: """Get and clear any pending message for a session.""" - return self._pending_messages.get(session_key) + return self._pending_messages.pop(session_key, None) def build_source( self, diff --git a/gateway/run.py b/gateway/run.py index 81c5dcf09..bad88fc2b 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -572,6 +572,13 @@ class GatewayRunner: if pending: print(f"[gateway] 📨 Processing interrupted message: '{pending[:40]}...'") + + # Clear the adapter's interrupt event so the next _run_agent call + # doesn't immediately re-trigger the interrupt before the new agent + # even makes its first API call (this was causing an infinite loop). + if adapter and hasattr(adapter, '_active_sessions') and source.chat_id in adapter._active_sessions: + adapter._active_sessions[source.chat_id].clear() + # Add an indicator to the response if response: response = response + "\n\n---\n_[Interrupted - processing your new message]_"