fix(discord): stop phantom typing indicator after agent turn completes (#3003)

Two fixes for a race where Discord's typing indicator lingers after the
agent finishes:

1. _keep_typing (root cause): after outer stop_typing() clears the task
   dict, _keep_typing wakes from its 2s sleep and calls send_typing()
   again, recreating an orphaned loop. Add a finally block so _keep_typing
   always calls stop_typing() on exit, cleaning up any loop it recreated.

2. _process_message_background (safety net): add stop_typing() after
   cancelling the typing task, catching any platform-level persistent
   typing tasks that slipped through.

Combines fixes from PR #2945 by catbusconductor (root cause in
_keep_typing) and PR #2832 by subrih (safety net in
_process_message_background).
This commit is contained in:
Teknium
2026-03-25 11:28:28 -07:00
committed by GitHub
parent 650b400c98
commit 65dace1b1a

View File

@@ -819,6 +819,16 @@ class BasePlatformAdapter(ABC):
await asyncio.sleep(interval)
except asyncio.CancelledError:
pass # Normal cancellation when handler completes
finally:
# Ensure the underlying platform typing loop is stopped.
# _keep_typing may have called send_typing() after an outer
# stop_typing() cleared the task dict, recreating the loop.
# Cancelling _keep_typing alone won't clean that up.
if hasattr(self, "stop_typing"):
try:
await self.stop_typing(chat_id)
except Exception:
pass
async def handle_message(self, event: MessageEvent) -> None:
"""
@@ -1130,6 +1140,13 @@ class BasePlatformAdapter(ABC):
await typing_task
except asyncio.CancelledError:
pass
# Also cancel any platform-level persistent typing tasks (e.g. Discord)
# that may have been recreated by _keep_typing after the last stop_typing()
try:
if hasattr(self, "stop_typing"):
await self.stop_typing(event.source.chat_id)
except Exception:
pass
# Clean up session tracking
if session_key in self._active_sessions:
del self._active_sessions[session_key]