Skip to content

fix(adapter): fire session.close in daemon thread to unblock event loop#1953

Open
chiefmojo wants to merge 1 commit into
MemTensor:dev-20260604-v2.0.19from
chiefmojo:fix/session-close-blocking
Open

fix(adapter): fire session.close in daemon thread to unblock event loop#1953
chiefmojo wants to merge 1 commit into
MemTensor:dev-20260604-v2.0.19from
chiefmojo:fix/session-close-blocking

Conversation

@chiefmojo

Copy link
Copy Markdown

Problem

The Hermes adapter calls session.close synchronously via requests.post on the asyncio event loop thread. When the bridge shuts down or becomes unresponsive the HTTP call blocks, stalling Discord heartbeats and causing a disconnect.

Fix

Move session.close off the event loop using loop.run_in_executor(None, ...). The call runs in the default thread-pool executor so asyncio heartbeats continue while the request is in flight. A 10-second timeout caps the wait.

Related

Companion to #1799 (fix(bridge): add shutdown timeout to prevent orphan bridge processes), which handles the bridge-side half of the same shutdown problem. Both PRs address the same orphan-accumulation incident from opposite ends.

on_session_end() called bridge.request("session.close") inline with a
30 s blocking urlopen(). gateway/run.py calls this synchronously from
_handle_reset_command (an async fn), so the blocking I/O ran on the
asyncio event loop thread, preventing Discord heartbeats from firing and
causing forced reconnection after 10–30 s.

The session.close response is unused and errors are already suppressed,
so the call is semantically fire-and-forget. Moving it to a daemon thread
is the correct fix: the event loop is never blocked, the request still
goes out, and a 5 s timeout keeps it bounded if the bridge is dead.

Reproducer: Violet 2026-06-12 09:54 (agent.log lines 3629–3791).
Spec: ~/specs/memos-bridge-blocking-shutdown-spec.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant