Skip to content

[homeassistant] fix: bound every wedgeable await during Home Assistant connect and teardown - #6

Closed
teknium1 wants to merge 1 commit into
mainfrom
port/homeassistant-1
Closed

teknium1 wants to merge 1 commit into
mainfrom
port/homeassistant-1

Conversation

@teknium1

@teknium1 teknium1 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Ported from NousResearch/hermes-homeassistant#1 by @Sora-bluesky. The hermes-homeassistant repo moved into this monorepo (see the README), so this is the same commit applied under homeassistant/, authorship kept. Review continues here.


Original description:

What does this PR do?

Ports NousResearch/hermes-agent#80372 to this repo, as suggested when that PR was closed after Home Assistant moved out of core.

The adapter could go silently deaf (NousResearch/hermes-agent#67470): several awaits on the connect and teardown paths have no bound, so a hung auth handshake or a wedged WebSocket or session close leaves the adapter stuck while the gateway still reports it as running. On main here these are the three receive_json() calls in _ws_connect (adapter.py:159, :163, :167), obj.close() in _close (:180) and the listen-task wait in disconnect (:193).

  • _ws_connect() builds the session into a local, bounds ws_connect and every handshake send and receive with _HANDSHAKE_TIMEOUT (30 s), and runs the bounded close on the local socket and session on failure, timeout or cancellation. self._session and self._ws are only set once the handshake has succeeded.
  • Every explicit close on the WebSocket connect and disconnect paths goes through _run_bounded_close() (_DRAIN_TIMEOUT, 5 s). The async with sessions in send() and _standalone_send() are not changed. A close that does not finish in time is logged and abandoned, and its task stays referenced until it ends.
  • disconnect() cancels the listen task through _cancel_task_bounded(), so a task that ignores cancellation cannot hold teardown.

There is no watchdog or stall detection in this PR, only the bounded waits.

Related Issue

NousResearch/hermes-agent#67470 (closed there when the adapter moved). Port of NousResearch/hermes-agent#80372.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)

Changes Made

  • adapter.py (+144/-30): the #80372 diff merged three-way onto this repo's adapter. The one overlap, the new module constants next to NOTIFICATION_TITLE, keeps both sides. _connect_error_detail, Platform("homeassistant") and the registration in __init__.py are untouched.
  • tests/test_homeassistant_network_reconnect.py (new): the 15 tests from #80372 with the import switched to homeassistant_plugin. One of them, test_cancel_task_bounded_abandons_uncancellable_task, now watches the helper with asyncio.wait and releases its uncancellable task in a finally, so a regression fails the test instead of hanging it.

How to Test

  1. PYTHONPATH=/path/to/hermes-agent python -m pytest tests -q: 78 passed, 2 skipped here (Windows 11, Python 3.11, hermes-agent main at 8b66a51036).
  2. With adapter.py put back to main's version, the new file fails 15 of 15.
  3. With the bounded wait in _cancel_task_bounded replaced by a plain await task, test_cancel_task_bounded_abandons_uncancellable_task fails in about 2 s instead of hanging.
  4. ruff check adapter.py tests/test_homeassistant_network_reconnect.py is clean.

Not exercised against a live Home Assistant instance; the tests drive fake sockets and sessions.

Checklist

  • My commit message follows Conventional Commits
  • I searched this repo's existing PRs (none open)
  • My PR contains only changes related to this fix
  • I've run pytest tests -q and all tests pass
  • I've added tests for my changes
  • I've tested on my platform: Windows 11

Infographic

pantheon-port

…ardown

Port of NousResearch/hermes-agent#80372, closed there when Home Assistant
moved to this plugin. The WebSocket handshake, the explicit closes on the
connect and disconnect paths and the listen-task cancel in disconnect()
now have a deadline. A failed or cancelled connect runs the same bounded
close on its local session, and a close that does not finish in time stays
tracked instead of being awaited forever, so a hung handshake or a wedged
close can no longer leave the adapter stuck (NousResearch/hermes-agent#67470).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@teknium1

Copy link
Copy Markdown
Contributor Author

Superseded: salvaged onto current main as #432, which landed via #446 (tip 0976e6d).

@teknium1 teknium1 closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

homeassistant Home Assistant plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants