fix(homeassistant): cleanup ClientSession when websocket handshake fails#29388
Open
StartupBros wants to merge 1 commit into
Open
fix(homeassistant): cleanup ClientSession when websocket handshake fails#29388StartupBros wants to merge 1 commit into
StartupBros wants to merge 1 commit into
Conversation
A failure between ``aiohttp.ClientSession.ws_connect`` and the final ``subscribe_events`` ack used to leave ``self._session`` open. The next reconnect would then assign over the live handle and the old session would be garbage-collected with its TCP connection still in CLOSE_WAIT, surfacing later as cryptic ``"Session is closed"`` or ``"Connector is closed"`` aiohttp errors on subsequent requests. Wrap the whole handshake in try/except so that any error tears down the ``ClientSession`` (and the WebSocket if it opened) via ``_cleanup_ws()`` before re-raising. ``self._ws`` and ``self._session`` are reset to ``None`` so the next ``_ws_connect`` attempt starts from a clean slate. Tests in ``TestWsConnectLifecycle`` cover both failure points: - ``ws_connect`` itself raising (TCP reset before handshake starts) - ``receive_json`` raising mid-handshake (peer closes after the connect)
This was referenced May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Split out from #29199 per CONTRIBUTING.md ("one logical change per PR"). #29199 now scopes to the env-var split only; this PR is the websocket connection-lifecycle fix it originally bundled.
Problem
HomeAssistantAdapter._ws_connectopens anaiohttp.ClientSessionand then runs a 4-step handshake (ws_connect→auth_required→auth→subscribe_events). Any of those steps can raise — TCP reset, peer-closed-mid-handshake, JSON decode error — but the failure path didn't run_cleanup_ws(). The leakedClientSessionthen bites on the next reconnect attempt: the new attempt assigns overself._sessionwhile the old one is still open, and the old one is GC'd with its TCP connection still inCLOSE_WAIT. Later requests on the new session surface as cryptic"Session is closed"or"Connector is closed"errors that don't point at the original handshake failure.Fix
Wrap the whole handshake in
try/except Exception:so that any error tears down theClientSession(and theWebSocketif it opened) via_cleanup_ws()before re-raising.self._wsandself._sessionare reset toNone, so the next_ws_connectattempt starts from a clean slate.The
exceptblock re-raises after cleanup — callers (connect()) keep their own broad-exceptthat turns this into a return-Falsefor the reconnect ladder; cleanup is purely additive.Test plan
New
TestWsConnectLifecycleclass intests/gateway/test_homeassistant.pycovers both failure points:test_ws_connect_failure_cleans_up_session—ClientSession.ws_connectraises before the handshake starts; session is closed,self._sessionisNone.test_auth_handshake_failure_cleans_up_ws_and_session—ws_connectsucceeds butreceive_jsonraises mid-handshake; both theWebSocketand theClientSessionget closed.Full file: 47 tests, all green.