Skip to content

Fixed race condition in Generic Thermostat#15784

Merged
amelchio merged 2 commits intohome-assistant:devfrom
aronsky:generic-thermostat/fix-race-condition
Aug 12, 2018
Merged

Fixed race condition in Generic Thermostat#15784
amelchio merged 2 commits intohome-assistant:devfrom
aronsky:generic-thermostat/fix-race-condition

Conversation

@aronsky
Copy link
Copy Markdown
Contributor

@aronsky aronsky commented Aug 1, 2018

Description:

Generic Thermostat has a race condition in its keepalive feature when the switching of the heater and the keepalive event happen at the same time. Here's a basic outline of the problem:

  1. Keepalive event begins executing, and checks which command to repeat (keepalive), based on the current status
  2. Context switches to the event of switching the heater. The event completes, and the heater is switched to the opposite state.
  3. Context returns to the keepalive event - which now retransmits the command of the old state, reverting the switch that just happened.

On top of the almost instantaneous turning on and off of the compressor (which can't be very healthy for it), the heater gets stuck in the old state for another min_cycle_duration, causing the temperature to drift further away from the target.

The issue is solved in this pull request by introducing a lock. Due to the async nature of the lock, the _async_control_heating is rewritten as async. Consequently, all methods that it calls are made async, and all the methods that call those methods, and so on. Currently, there's a mix of async and regular methods throughout the code - it's messy, but will be addressed in a future pull request, to limit the number of reviewable changes in this PR.

Additionally, _async_control_heating was refactored to be more concise, and accommodate the old keepalive functionality (the old async_keep_alive is now obsolete and, therefore, removed).

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

@aronsky
Copy link
Copy Markdown
Contributor Author

aronsky commented Aug 3, 2018

@amelchio @balloob @MartinHjelmare
It's a smaller PR than the previous one, with minimal changes only relevant to the fix itself. What do you think?

Copy link
Copy Markdown
Contributor

@amelchio amelchio left a comment

Choose a reason for hiding this comment

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

Looks good to me.

I enjoyed your thorough description, it makes reading the diff that much easier.

self.heater_entity_id)
self._heater_turn_off()
await self._async_heater_turn_off()
elif time is not None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks a bit odd, maybe add a comment or named variable that tells the reader that this is the keepalive case?

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.

You're right, I'll add a comment.

(not self.ac_mode and too_cold):
_LOGGER.info("Turning on heater %s", self.heater_entity_id)
self._heater_turn_on()
await self._async_heater_turn_on()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You have removed the logging that mentions AC and I think that is okay because the configuration for the entity_id is indeed named heater, even in ac_mode.

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.

Yup. I hope we can come up with a better, more neutral naming convention (as heater is very, well, heating-oriented :) ), and when we do, I'll update the logging accordingly.

@amelchio amelchio merged commit b7486e5 into home-assistant:dev Aug 12, 2018
@ghost ghost removed the in progress label Aug 12, 2018
@aronsky aronsky deleted the generic-thermostat/fix-race-condition branch August 18, 2018 06:56
@balloob balloob mentioned this pull request Aug 29, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
* Fixed race condition in Generic Thermostat

* Added a comment to clarify the meaning of the `time` argument.
vrih pushed a commit to vrih/home-assistant that referenced this pull request Sep 29, 2018
* Fixed race condition in Generic Thermostat

* Added a comment to clarify the meaning of the `time` argument.
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
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.

3 participants