Repository navigation
Description
title: "fix(gateway): Feishu session cancellation can orphan session guard, permanently blocking subsequent messages"
labels: ["bug", "gateway", "feishu"]
Description
When a Feishu DM session is cancelled (e.g., by a /stop or /new command from another platform), cancel_session_processing() in gateway/platforms/base.py waits up to 5s for the old task to exit. If the task doesn't exit within 5s (asyncio.TimeoutError):
- The task is removed from
_session_tasksbut continues running (orphaned) - When it eventually finishes, its
finallyblock checksself._session_tasks.get(session_key) is current_task— this is False because the task was removed, so_release_session_guard()is never called _active_sessions[session_key]is never released- All subsequent messages for that session go into
_pending_messagesbut nobody ever processes them
Impact
- Feishu DM stops responding after a
/stopor/newfrom CLI/another platform - Session shows
input_tokens: 0, output_tokens: 0, total_tokens: 0(empty shell) - Affected user must wait for gateway restart or session timeout to recover
- Particularly common when multiple platforms access the same session
Reproduction
- Send a message to Feishu that starts a long-running task
- While it's running, send
/stopor/newfrom CLI - If the old task takes >5s to cancel, the session guard is orphaned
- Send a new Feishu message — it's queued but never processed
- Check gateway.log:
"Cancelled task for ... did not exit within 5s; unblocking dispatch and letting the task unwind in the background"
Root Cause
https://github.com/NousResearch/hermes-agent/blob/v2026.5.28/gateway/platforms/base.py#L3209-L3237
except asyncio.TimeoutError:
logger.warning(
"[%s] Cancelled task for %s did not exit within 5s; "
"unblocking dispatch and letting the task unwind in the background",
self.name, session_key,
)
# ← NO cleanup of _active_sessions or _expected_cancelled_tasks here
# ...
if release_guard:
self._release_session_guard(session_key)
# ← NO orphan message respawn logicTwo missing pieces:
- Cleanup on timeout:
_expected_cancelled_tasks.discard(task)+_active_sessions.pop(session_key, None)in the TimeoutError handler - Respawn on release: Check
_pending_messagesafterrelease_guardand respawn if orphaned messages are queued
Proposed Fix
Part 1: Clean up orphaned references on timeout
except asyncio.TimeoutError:
logger.warning(
"[%s] Cancelled task for %s did not exit within 5s; "
"forcing session release and respawning pending messages",
self.name, session_key,
)
self._expected_cancelled_tasks.discard(task)
self._active_sessions.pop(session_key, None)Part 2: Respawn queued messages after guard release
if release_guard:
if session_key in self._pending_messages and session_key not in self._active_sessions:
pending = self._pending_messages.pop(session_key, None)
if pending is not None:
logger.info(
"[%s] Respawning pending message for orphaned session %s",
self.name, session_key,
)
self._start_session_processing(pending, session_key)
self._release_session_guard(session_key)Related Issues
Issue 2a: Feishu approval card unauthorized in DM
In gateway/platforms/feishu.py, approval card button clicks (Always Approve / Deny) are checked via _allow_group_message():
if not self._allow_group_message(sender_id, state.get("chat_id", ""), is_bot=False):
logger.warning("[Feishu] Unauthorized approval click by %s", open_id)
returnThis function is designed for group chat access control. In DM context, when FEISHU_GROUP_POLICY defaults to "allowlist" and FEISHU_ALLOWED_USERS is unset, every user is blocked from approving. The fix is to use a DM-aware authorization check that defaults to permissive in DM:
def _is_interactive_operator_authorized(self, open_id: str) -> bool:
admins = self.config.get("admins", [])
allowed = self.config.get("allowed_users", [])
if not admins and not allowed:
return True # No restrictions configured → allow everyone
return open_id in admins or open_id in allowedIssue 2b: Feishu WebSocket lacks ping/heartbeat
The Feishu platform adapter does not configure WebSocket ping intervals. On unstable networks, the WebSocket connection can enter a zombie state — TCP shows ESTABLISHED but data stops flowing. Messages sent by the user are silently lost. The fix is to add ws_ping_interval and ws_ping_timeout as configurable env vars:
# In gateway/config.py _apply_env_overrides(), add to Feishu extra dict:
config.platforms[Platform.FEISHU].extra["ws_ping_interval"] = int(os.getenv("FEISHU_WS_PING_INTERVAL", "0")) or None
config.platforms[Platform.FEISHU].extra["ws_ping_timeout"] = int(os.getenv("FEISHU_WS_PING_TIMEOUT", "0")) or NoneTogether with FEISHU_WS_PING_INTERVAL=30 and FEISHU_WS_PING_TIMEOUT=10 in .env, this keeps the WebSocket alive.
Environment
- Hermes Agent v2026.5.28 (present in all versions)
- Feishu/Lark platform with WebSocket connection mode
- Multi-platform usage (Feishu + CLI)
Activity
teknium1 commentedon Jul 1, 2026
Closing as fixed on
main. This orphan-guard deadlock (cross-platform/stopleaving_active_sessionspointing at a timed-out task, so later messages pile into_pending_messageswith no runner) no longer reproduces on currentmain.Three fixes landed after this was reported that together cover it:
_heal_stale_session_lock()— on-entry self-heal clears a stale session guard when the owner task is already done (bug(gateway): stale adapter busy lock can trap Telegram sessions in repeated "Interrupting current task..." while /stop says "No active task to stop" #11016)._cleanup_finished_session_task()— release-then-conditional-delete keeps the done-task entry so the stale lock stays detectable and gets healed (bug(gateway): _session_task_is_stale misses stale lock when task entry already cleaned up #48300).task.add_done_callback(self._expected_cancelled_tasks.discard)— the orphan auto-clears from_expected_cancelled_taskson exit.Verified empirically against unpatched current
main:/stopfrom another platform + queued follow-up →_drain_pending_after_session_commandrespawns the pending message. No orphan./stop+ no pending → guard released cleanly.cancel_session_processing(release_guard=True)timeout →_release_session_guardclears_active_sessions;_expected_cancelled_tasksauto-clears via the done-callback.Thanks @ccwssy for the precise root-cause and @Bartok9 for the fix PR (#34279) — your diagnosis was exactly right; the codebase just landed the fix along a different code path in the meantime. Reopen if you can still reproduce on latest
main.