Skip to content

Fix encryption disconnect race #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2025
Merged

Fix encryption disconnect race #355

merged 4 commits into from
Jun 9, 2025

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Jun 9, 2025

replaces and closes #354

should fix the issue in home-assistant/core#142316 (comment)

Copy link

codecov bot commented Jun 9, 2025

Codecov Report

Attention: Patch coverage is 74.54545% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
switchbot/devices/device.py 74.54% 14 Missing ⚠️
Files with missing lines Coverage Δ
switchbot/devices/device.py 57.72% <74.54%> (+15.32%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco requested a review from Copilot June 9, 2025 15:38
@bdraco bdraco marked this pull request as ready for review June 9, 2025 15:38
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors command sending and encryption initialization to avoid race conditions during disconnect by introducing an operation lock and a unified retry helper.

  • Introduces _send_command_locked_with_retry and updates _send_command to use it under self._operation_lock.
  • Ensures encryption is initialized inside the lock and adds guard clauses for IV state.
  • Clears encryption state under a connect lock during disconnect.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_encrypted_device.py Adds tests for the new locking behavior, encryption init, and race handling on disconnect
switchbot/devices/device.py Refactors _send_command, adds _send_command_locked_with_retry, strengthens encryption init, and updates disconnect logic
Comments suppressed due to low confidence (2)

switchbot/devices/device.py:206

  • The parameter 'retry' is not used in this method and can be removed to simplify the signature and avoid confusion.
async def _send_command_locked_with_retry(self, key: str, command: bytes, retry: int, max_attempts: int) -> bytes | None:

switchbot/devices/device.py:206

  • [nitpick] Consider adding a docstring to explain the purpose and usage of this helper method.
async def _send_command_locked_with_retry(self, key: str, command: bytes, retry: int, max_attempts: int) -> bytes | None:

@bdraco bdraco merged commit 1d86e19 into master Jun 9, 2025
4 of 5 checks passed
@bdraco bdraco deleted the robust_fix_disconnect branch June 9, 2025 15:49
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