MCP keepalive bugs, reconnect loops, stale connections
NousResearch/hermes-agent · cluster 17003 · mcp
Data as of
Nothing open on GitHub. Fix merged in #20209. Check it covers your case.
The short answer
- Known?
- Yes. 10 related items grouped here, 3 issues already closed.
- Fix event
- A fix was merged to main in PR #20209. Check it covers your case. No release data is tracked.
- Tracker
- The maintainers' tracker marks this
partially resolved, deciding PRs #20209, #68658. That is separate from the fix event. - Still open
- Nothing open on GitHub.
These issues and fixes address multiple related problems in MCP keepalive logic: inefficient list_tools-based probing, infinite reconnect loops triggered by empty exceptions, and stale HTTP connections after idle periods. Several fixes attempt to address overlapping aspects of the same fragile keepalive mechanism.
Open issues0
Open PRs0
Fixes on record
-
#20209 fix(mcp): add periodic keepalive to _wait_for_lifecycle_event (salvage #17016)
files:
tools/mcp_tool.pyShow the diff
-
tools/mcp_tool.py@@ -1038,14 +1038,43 @@ async def _wait_for_lifecycle_event(self) -> str: with a fresh signal. Shutdown takes precedence if both events are set simultaneously. + + Periodically sends a lightweight keepalive (``list_tools``) to + prevent TCP connections from going stale during long idle + periods (#17003). If the keepalive fails, triggers a reconnect. """ + # Keepalive interval in seconds. Must be shorter than typical + # LB / NAT idle-timeout (commonly 300-600s). + _KEEPALIVE_INTERVAL = 180 # 3 minutes + shutdown_task = asyncio.create_task(self._shutdown_event.wait()) reconnect_task = asyncio.create_task(self._reconnect_event.wait()) try: - await asyncio.wait( - {shutdown_task, reconnect_task}, - return_when=asyncio.FIRST_COMPLETED, - ) + while True: + done, _pending = await asyncio.wait( + {shutdown_task, reconnect_task}, + timeout=_KEEPALIVE_INTERVAL, + return_when=asyncio.FIRST_COMPLETED, + ) + if done: + break + + # Timeout — no lifecycle event fired. Send a keepalive + # to exercise the connection and detect stale sockets. + if self.session: + try: + await asyncio.wait_for( + self.session.list_tools(), + timeout=30.0, + ) + except Exception as exc: + logger.warning( + "MCP server '%s' keepalive failed, " + "triggering reconnect: %s", + self.name, exc, + ) + self._reconnect_event.set() + break finally: for t in (shutdown_task, reconnect_task): if not t.done():
-
-
#68658 fix(mcp): budget-bounded reconnects, exception-group unwrapping, and per-transition logging
files:
tests/tools/test_mcp_failure_classification.py,tests/tools/test_mcp_rapid_drop_budget.py,tests/tools/test_mcp_reconnect_log_hygiene.py,tests/tools/test_mcp_transport_group_reconnect.py,tools/mcp_tool.py
5 PRs closed without merging
From the close comments: superseded by another PR 2 · done elsewhere 1 · merged via another change 1 · other 1
- #17016 fix: MCP circuit breaker recovery and HTTP keepalive (#16788, #17003) · other
-
#62220 fix(mcp): treat empty keepalive exceptions as ping unsupported to prevent infinite reconnect loops · merged via another change →
#68658,#62212 -
#62506 fix(mcp): prevent infinite keepalive reconnect loop on empty exceptions (#62212) · superseded by another PR →
#62515 -
#62515 fix(mcp): prevent infinite keepalive reconnect loop on empty exceptions (#62212) · superseded by another PR →
#64838 -
#65813 fix(mcp): increase list_tools keepalive timeout to 60s and improve error logging · done elsewhere →
#65787,#65795
Closed issues · 3
How this cluster was grouped
-
Link evidence (
partly structural links): some members are connected by structural evidence (shared changed files, or a closing/superseded reference); the rest are joined by an automated relevance judgement over their text. - Members outside that structural core may belong to a different underlying problem.
-
Pairwise link density
0.17: the share of item pairs that carry a link, counting a structural link in full and a relevance-only link at 0.36.1.00means every pair is linked.
Pick up a Lab task
Check whether a fix covers the open issues here, with your own agent. Bring back evidence for maintainers.
Open the Lab