fix(spotify): tear down errored backend in SpotifyController (no leak/cross-talk)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
saopig1andClaude Opus 4.8 committed 2026-07-02 21:30:13 +08:00
1 parent 3a9504500b
commit 179e7c248a
2 files changed
+87

No files matched your search

+19
View File
@@ -114,6 +114,25 @@ export class SpotifyController extends EventEmitter {
*/
private handleBackendError(err: unknown): void {
this.logger.error({ err }, "Spotify backend error; marking not-ready");
// Tear down the errored backend BEFORE resetting flags so ensureStarted()
// does not orphan it: stop() cleans its ffmpeg/go-librespot children + FIFO,
// removeAllListeners() detaches its "error" handler so a later error from
// this now-orphaned backend cannot flip a healthy rebuilt controller
// back to not-ready (state cross-talk). Teardown must never mask the
// original error, so guard stop() which may throw.
try {
this.backend?.stop();
} catch (stopErr) {
this.logger.error(
{ err: stopErr },
"Spotify backend stop() threw during error teardown",
);
}
// SpotifyAudioBackend's type contract exposes on() but not
// removeAllListeners(); every concrete backend extends EventEmitter, so
// detach through it to drop this controller's listeners from the orphan.
(this.backend as unknown as EventEmitter | null)?.removeAllListeners();
this.backend = null;
this.started = false;
this.startPromise = null;
}