Skip to content

Debounce calls to Plex server#33560

Merged
balloob merged 5 commits intohome-assistant:devfrom
jjlawren:plex_debounce
Apr 4, 2020
Merged

Debounce calls to Plex server#33560
balloob merged 5 commits intohome-assistant:devfrom
jjlawren:plex_debounce

Conversation

@jjlawren
Copy link
Copy Markdown
Contributor

@jjlawren jjlawren commented Apr 3, 2020

Proposed change

Updates for Plex media_player and sensor entities are triggered whenever a callback is received on a websocket listener. Sometimes these can come in rapid succession and it appears they can cause race conditions where objects are updated while they're being used. It's important to throttle the updates but also not "lose" the last request to update as updates can arrive very infrequently.

A different approach to #33534 which I believe is addressing a symptom instead of the root cause.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

Comment thread homeassistant/components/plex/server.py
Comment thread homeassistant/components/plex/server.py Outdated
@jjlawren jjlawren marked this pull request as ready for review April 4, 2020 03:29
@balloob balloob added this to the 0.108.0 milestone Apr 4, 2020
@balloob balloob merged commit 4235328 into home-assistant:dev Apr 4, 2020
Comment thread homeassistant/components/plex/server.py
balloob pushed a commit that referenced this pull request Apr 5, 2020
* Debounce calls to Plex server

* Simplify debounce by recommendation

* Update tests to handle debounce

* Test debouncer, fix & optimize tests

* Use property instead
if unsub:
_LOGGER.debug("Throttling update of %s", self.friendly_name)
unsub() # pylint: disable=not-callable
unsub = async_call_later(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is calling an async method from a sync context.

nonlocal unsub
if unsub:
_LOGGER.debug("Throttling update of %s", self.friendly_name)
unsub() # pylint: disable=not-callable
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unsub functions should only be called from the same context as in which they are set. So if created with async_call_later, need to be called from inside the event loop.

Also, because this is running in a thread, you cannot guarantee that nothing else touches it. You should instead run this all inside the event loop.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's better to turn this the wrapper function into an async function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Really sorry about that, hopefully fixed in #33730.

@jjlawren jjlawren mentioned this pull request Apr 6, 2020
20 tasks
@jjlawren jjlawren deleted the plex_debounce branch April 6, 2020 03:10
@jjlawren jjlawren mentioned this pull request Apr 7, 2020
20 tasks
@lock lock Bot locked and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants