Skip to content

Close the Chromecast connection once Spotify has taken over - #81

Merged
Mincka merged 1 commit into
mainfrom
fix/80-cast-connection-leak
Oct 27, 2026
Merged

Mincka merged 1 commit into
mainfrom
fix/80-cast-connection-leak

Conversation

@Mincka

@Mincka Mincka commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Fixes #80

Problem

Every play service call that targets a Google Cast entity builds a new pychromecast connection in async_build_from_type, and nothing ever closes it. pychromecast's socket client retries forever until it is stopped, so each call leaves a thread reconnecting to the device for as long as Home Assistant runs. After enough calls the device refuses new connections and every later cast to it fails until Home Assistant restarts.

Fix

  • MediaPlayer.release(), a no-op by default. Chromecast.release() calls socket_client.disconnect(), which stops the thread without waiting for it. Chromecast.disconnect(timeout=0) cannot be used: pychromecast 14.0.10 joins the thread and raises TimeoutError while it is still winding down, which would turn a successful play into an error.
  • async_build_from_type releases the player on any failure during connect or launch, cancellation included.
  • play_media and play_custom_context start playback through a new async_play_and_release helper that releases the player after playback and extras are applied, and when they fail. Every other play service goes through play_media.

The connection is closed only after Spotify has the playback, so the receiver app is never left without a sender while it is still starting up.

Testing

  • 12 new unit tests, all failing on main. The Chromecast.release() test runs pychromecast's real socket thread against a closed port and checks that it stops. Full suite: 853 passed; pylint 10.00.
  • Live on Home Assistant with a Nest Audio, counting Home Assistant's established connections to the device's port 8009 after each spotcast.play_media call:
before (6.6.4) after
baseline 1 1
call 1 (cold Spotify launch) 2 1
call 2 3 1
call 3 4 1

The remaining connection is Home Assistant's own cast entity. Spotify was still playing on the device about 25 s after each release. play_custom_context and play_liked_songs were checked the same way.

🤖 Generated with Claude Code

async_build_from_type opened a new pychromecast connection for every
service call that targeted a Google Cast entity and never closed it.
pychromecast's socket client retries forever unless it is stopped, so
each call left a thread reconnecting to the device for the life of the
process. A speaker that received a few plays a day collected dozens of
connections until it refused new ones (#80).

MediaPlayer gains release(), a no-op by default. Chromecast.release()
stops the socket client without joining it: disconnect(timeout=0)
raises TimeoutError from its join while the thread is still winding
down, which would turn a successful play into an error.

async_build_from_type releases the player on any failure, including
cancellation. play_media and play_custom_context, which every play
service goes through, now start playback through
async_play_and_release, which releases the player once playback and
extras are applied, and also when they fail. The connection stays open
until Spotify has the playback, so the receiver app is never left
without a sender before then.

Verified live against a Nest Audio: on 6.6.4, Home Assistant's
connections to the device went 1, 2, 3, 4 over three play_media calls;
with this change they stayed at 1 (Home Assistant's own cast entity),
and Spotify kept playing after each release, including a cold launch.

Fixes #80

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mincka
Mincka merged commit d5bd80e into main Oct 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

play_media to a Cast device leaks a pychromecast connection per call; device eventually refuses new connections

1 participant