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

Fix MQTT discovery #17004

Merged
merged 1 commit into from
Oct 1, 2018
Merged

Conversation

OttoWinter
Copy link
Member

@OttoWinter OttoWinter commented Sep 30, 2018

Description:

After looking into #16968 a bit more (so that the device registry will be possible for MQTT entities 😉), and finding out what exactly was causing the problem, I think I now have a working fix that does not modify any core code.

The problem was that the MQTT discovery callback would get called multiple times while the platform was still being set up. Inside async_forward_entry_setup and then in async_setup_component a new task is created for the loading of each component. Until the config entry is fully forwarded, another MQTT discovery message might come in and call async_forward_entry_setup another time, triggering the bug in #16968

Suppose a discovery message for a switch.mqtt comes in. The platform needs to be loaded and the config entry is forwarded via async_forward_entry_setup. async_forward_entry_setup however adds new tasks to the event queue and the key is only added to CONFIG_ENTRY_IS_SETUP after that newly created task is completed.

During that time, another discovery message for another switch might come in. Nothing is in CONFIG_ENTRY_IS_SETUP yet so the platform is loaded again, triggering #16968

This PR shuffles the affected line around and I have since not been able to reproduce the race condition. Because this code fragment is now executed in one go and lives inside the event queue, I think the race condition should be fixed.

Related issue (if applicable): fixes #16968

Checklist:

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

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@OttoWinter OttoWinter requested a review from a team as a code owner September 30, 2018 14:42
@ghost ghost assigned OttoWinter Sep 30, 2018
@ghost ghost added the in progress label Sep 30, 2018
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.

One potential race condition could be that the config entry set up is not yet listening to dispatched signals.

This fix is a good start though.

@balloob balloob merged commit 760f822 into home-assistant:dev Oct 1, 2018
@ghost ghost removed the in progress label Oct 1, 2018
@balloob balloob mentioned this pull request Oct 12, 2018
@OttoWinter OttoWinter deleted the fix-mqtt-discovery branch December 16, 2018 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MQTT Discovery broken after merge of #16904 / #16918
3 participants