Skip to content

Fix session consistency in request handler methods#23

Merged
lmaertin merged 1 commit intolmaertin:mainfrom
ronaldvdmeer:fix/request-handler-session-consistency
Mar 13, 2026
Merged

Fix session consistency in request handler methods#23
lmaertin merged 1 commit intolmaertin:mainfrom
ronaldvdmeer:fix/request-handler-session-consistency

Conversation

@ronaldvdmeer
Copy link
Contributor

Three methods in RequestHandler (set_value(), get_device_language(), reboot_device()) create their own aiohttp.ClientSession instead of using the shared _get_session() method. This bypasses externally provided sessions (e.g. from Home Assistant's aiohttp_client) and can cause resource leaks.

Additionally, get_access_point() has a double-read bug where resp.text() consumes the response body and the subsequent resp.json() can fail.

Changes

Session consistency (set_value, get_device_language, reboot_device)

  • Replaced standalone aiohttp.ClientSession(connector=connector) with await self._get_session()
  • Added proper try/finally blocks to close internally-created sessions
  • External sessions are no longer closed by these methods (matching existing behavior in get_raw_data, get_debug_config, etc.)

Double-read fix (get_access_point)

  • resp.text() already consumed the response body
  • Replaced resp.json() with json.loads(text[json_start:json_end]) to parse from the already-read text

Testing

  • Added 10 new tests covering:
    • External session passthrough (session not closed)
    • Internal session creation and cleanup
    • Session cleanup on errors
    • Access point text/JSON parsing
    • Non-JSON response handling
  • All 133 tests pass

…ice and get_access_point

- set_value(): Use _get_session() instead of creating a standalone
  aiohttp.ClientSession, so externally provided sessions (e.g. from
  Home Assistant) are properly used and internal sessions are closed.

- get_device_language(): Same fix - use _get_session() for session
  consistency with the rest of the request handler.

- reboot_device(): Same fix - use _get_session() for session
  consistency.

- get_access_point(): Fix double-read bug where resp.text() consumed
  the body and resp.json() would fail. Now parses JSON from the
  already-read text using json.loads().

Added 10 tests covering:
- External session passthrough (not closed by handler)
- Internal session creation and cleanup
- Session cleanup on errors
- Access point text/JSON parsing
@lmaertin
Copy link
Owner

Very clean enhancement, thanks for the tests as well - easy to merge.

@lmaertin lmaertin merged commit 93bbca3 into lmaertin:main Mar 13, 2026
6 of 9 checks passed
@ronaldvdmeer
Copy link
Contributor Author

Hi @lmaertin

Thanks for all the merges.

I noticed the CI checks failed after the merge. From the logs it looks like the pylint step might be complaining about a few unused variables after the switch to _get_session().

I also saw a reference to data not existing in one of the checks, so I just wanted to flag it in case it’s related to my change.

Happy to open a small follow-up PR if something needs to be cleaned up.

@lmaertin
Copy link
Owner

No Issue, I fixed this in the release already.

@ronaldvdmeer ronaldvdmeer deleted the fix/request-handler-session-consistency branch March 14, 2026 12:36
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.

2 participants