Skip to content
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

Add an asyncio Lock around pairing, which cant be used concurrently #21933

Merged
merged 1 commit into from Mar 11, 2019

Conversation

Jc2k
Copy link
Member

@Jc2k Jc2k commented Mar 11, 2019

Description:

I'll need to rebase this after #21901 is merged to fix a conflict in the update() method.

I was recently discussing plans for homekit_controller with another user/contributor and realised that there is no explicit locking around the pairing. The homekit_controller Pairing object is not safe to use concurrently, so users with bridges may be experiencing glitches as the asynchronicity trips up the session encryption.

I think there is already some logic around EntityPlatforms that limits concurrent polls (PARALLEL_UPDATES) which means you are unlikely to see problems with just polling. However if you have concurrent writes (or a write and poll at the same time) then you'll get a AccessoryDisconnected error and the request will fail. In one of my longer running branches to add BLE homekit support I actually noticed this quite a lot with even a single device. BLE requests are quite slow so multi tapping on UI elements was enough to trigger the problem.

It seemed like it would be easiest to reason about this if we used the async_ version of the entity API as much as possible and handled the locking needed for the pairing object on the HA side as close to the pairing object as possible. I also wanted an approach that played well with future plans to make an asyncio variant of homekit_python (the upstream we are using for this component).

I have tested this with a homekit_python demoserver.py setup as a fake bridge that has 50 fake light bulbs attached to it, and the tests pass locally.

As I was editing an adjacent function i've removed update_characteristics as its no longer used.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

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

Looking good 👍

@balloob balloob merged commit 5e2302e into home-assistant:dev Mar 11, 2019
@ghost ghost removed the in progress label Mar 11, 2019
@Jc2k Jc2k deleted the homekit_concurrency branch March 11, 2019 21:01
Jc2k added a commit to Jc2k/home-assistant that referenced this pull request Mar 12, 2019
@Jc2k Jc2k mentioned this pull request Mar 12, 2019
3 tasks
robbiet480 pushed a commit that referenced this pull request Mar 13, 2019
@balloob balloob mentioned this pull request Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants