music-assistant-server

Branch:dev-lidarr-plugin

Commit Details

Fix leaked aiohttp session when an AirPlay control connection drops (#6162)

# What does this implement/fix?

`_handle_connection_closed` nulled the pyatv device references and
returned without calling `close()`, so the `aiohttp.ClientSession` that
`pyatv.connect()` created was left open. pyatv does not close the facade
itself on connection loss. Nightly logs show `Unclosed client session`
errors traced back to `_connect_mrp`, and one Apple TV reconnected 181
times over MRP and 181 over Companion in the same 24h window.

Also present on `stable`, so it is a backport candidate.

**Related issue (if applicable):**

- n/a

## Types of changes

- [x] Bugfix (non-breaking change which fixes an issue) — `bugfix`
- [ ] New feature (non-breaking change which adds functionality) —
`new-feature`
- [ ] Enhancement to an existing feature — `enhancement`
- [ ] New music/player/metadata/plugin provider — `new-provider`
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected) — `breaking-change`
- [ ] Refactor (no behaviour change) — `refactor`
- [ ] Documentation only — `documentation`
- [ ] Maintenance / chore — `maintenance`
- [ ] CI / workflow change — `ci`
- [ ] Dependencies bump — `dependencies`

Changes:

- Close the facade for both the Companion and MRP branches, matching the
cleanup `_handle_push_error` already does.
- A close callback for an already-detached device still returns early
and never closes a live connection.
- Regression tests for both sources plus the detached-device case.

## Checklist

- [x] The code change is tested and works locally.
- [x] `pre-commit run --all-files` passes.
- [x] `pytest` passes, and tests have been added/updated under `tests/`
where applicable.
- [ ] For changes to shared models, the companion PR in
`music-assistant/models` is linked.
- [ ] For changes affecting the UI, the companion PR in
`music-assistant/frontend` is linked.
- [x] I have read and complied with the project's [AI
Policy](https://github.com/music-assistant/.github/blob/main/AI_POLICY.md)
for any AI-assisted contributions.
- [ ] I have [raised a PR against the documentation
repository](https://github.com/music-assistant/music-assistant.io/blob/main/CONTRIBUTING.md)
targeting the main or beta branch as appropriate.
Forgejo

Author

Marvin Schenkel<[email protected]>
September 2, 2026 at 01:32 PM UTC

Committer

Marvin Schenkel<[email protected]>
September 4, 2026 at 07:55 AM UTC
38additions
0deletions
2files changed

Parent commits

Changes

2 files
+38-0

Changed Files (2)

music_assistant/providers/airplay/
tests/providers/airplay/
music_assistant/providers/airplay/control_player.py
tests/providers/airplay/test_control_player.py
+38