Skip to content

Fix Sonos group regroup race when entity is not yet registered#169445

Merged
frenck merged 1 commit intodevfrom
fix-sonos-tests-169048
Apr 29, 2026
Merged

Fix Sonos group regroup race when entity is not yet registered#169445
frenck merged 1 commit intodevfrom
fix-sonos-tests-169048

Conversation

@epenet
Copy link
Copy Markdown
Contributor

@epenet epenet commented Apr 29, 2026

Breaking change

Proposed change

Re-enables the three sonos tests skipped in #169046 and fixes the underlying race they exposed.

SonosSpeaker.setup() schedules a polling-driven update_groups() coroutine and the platform-creating async_setup() task at the same time. The polling task calls _async_regroup, which looks up the media_player entity ID via async_get_entity_id. If platform setup has not yet registered the entity, that returns None, and None was being appended directly to sonos_group_entities as [None].

Worse, a later early-return condition (group == [self.soco.uid] and self.sonos_group == [self] and self.sonos_group_entities) treated [None] as truthy and skipped re-computing, so subsequent zone group events could not repair the state. The state then surfaced as group_members: [None] on the entity.

On Python 3.14.3 the asyncio executor scheduling change (cpython#142358) made await hass.async_add_executor_job(...) no longer yield to the loop, so the polling regroup deterministically wins the race against entity registration. This is why the three tests started failing.

Fix: in _async_regroup, treat a missing entity_id the same as a missing speaker — mark the uid as missing and return, so the next event retries. This avoids ever storing None in sonos_group_entities.

Also, test_subscription_repair_issues needs wait_background_tasks=True after reload so the new async_subscription_failed fires before the test invokes the success callback (otherwise it can re-create the issue after the deletion and fail the assertion).

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)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

A polling-driven update_groups() can run before the media_player entity
has been registered. async_get_entity_id then returns None, which was
appended into sonos_group_entities as [None]. On subsequent zone group
events the early-return condition (which checks `and self.sonos_group_entities`)
treated [None] as truthy and skipped re-computing, leaving the bad state
permanently. This race became deterministic on Python 3.14.3 due to the
asyncio executor scheduling change (cpython#142358), causing three sonos
tests to be skipped in #169046.

Treat a missing entity_id the same as a missing speaker: mark as
"members missing" and return so the next event retries.

Also wait for background tasks after reload in
test_subscription_repair_issues so the v2 async_subscription_failed
fires before the test calls the success callback.
Copilot AI review requested due to automatic review settings April 29, 2026 10:59
@home-assistant home-assistant Bot added bugfix cla-signed has-tests integration: sonos small-pr PRs with less than 30 lines. Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage Top 50 Integration is ranked within the top 50 by usage labels Apr 29, 2026
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @jjlawren, @PeteRager, mind taking a look at this pull request as it has been labeled with an integration (sonos) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of sonos can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign sonos Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

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

Fixes a Sonos regroup race where sonos_group_entities could incorrectly contain None when a topology update runs before the media_player entity is registered, and re-enables the previously skipped tests that exposed the issue on Python 3.14.3+.

Changes:

  • Prevent None from being added to sonos_group_entities by treating a missing entity_id as a missing group member and retrying later.
  • Re-enable three Sonos tests that were skipped due to Python 3.14.3 asyncio scheduling behavior.
  • Stabilize test_subscription_repair_issues by waiting for background tasks after reload and after invoking the subscription callback.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
homeassistant/components/sonos/speaker.py Avoids persisting invalid group member entity IDs during regroup when entity registration hasn’t completed yet.
tests/components/sonos/test_speaker.py Re-enables the ZGS grouping test previously skipped for Python 3.14.3 asyncio behavior.
tests/components/sonos/test_media_player.py Re-enables the basic entity snapshot test previously skipped for Python 3.14.3 asyncio behavior.
tests/components/sonos/test_repairs.py Re-enables repairs test and ensures async background work completes before assertions.

Copy link
Copy Markdown
Contributor

@PeteRager PeteRager left a comment

Choose a reason for hiding this comment

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

Nice work, thank you!

@epenet epenet marked this pull request as ready for review April 29, 2026 11:07
@epenet epenet requested a review from jjlawren as a code owner April 29, 2026 11:07
Copy link
Copy Markdown
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @epenet 👍

../Frenck

                       

Blogging my personal ramblings at frenck.dev

@frenck frenck merged commit 2da4400 into dev Apr 29, 2026
37 checks passed
@frenck frenck deleted the fix-sonos-tests-169048 branch April 29, 2026 12:14
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix cla-signed has-tests integration: sonos Quality Scale: bronze small-pr PRs with less than 30 lines. Top 50 Integration is ranked within the top 50 by usage Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix sonos tests broken by Python 3.14.3 asyncio changes

5 participants