Skip to content

Conversation

@aspage2
Copy link
Owner

@aspage2 aspage2 commented Mar 21, 2025

Bug Fix: Runaway Goroutines

The startMPDIdler function has a bug which causes the idler goroutine to enter an infinite loop if its connection to MPD is broken. The issue is an incorrectly-used scanner; in the case that MPD closes the connection, sc.Scan() returns false but doesn't error. The Idler only exits on error, so it enters an infinite loop of calling sc.Scan() on a closed socket.

Fixes

  • Switch to a bufio.Reader for error-handling at the time that the socket read occurs, as well as direct handling of io.EOF.
  • Add error-handling code where there was none previously, returning from the idler if any socket error occurs
  • Wrap the original idler logic in another function that manages dropped connections and re-attempts to connect to MPD
  • Include a Sleep call to throttle the attempts to reconnect.

New Event Pub/Sub System

Create a new Topic type which supports single-topic publish/subscribe between goroutines.
Receivers can subscribe and unsubscribe from the topic, and publishers can create events on
the topic with Publish. Topic is thread-safe.

Refactored the /go/events endpoint to utilize a pub/sub topic to receive updates from the server.
Each SSE client subscribes to the topic, and the new "idler" goroutine publishes changes on the other end.

Refactor: Single idler, multiple listeners

Currently, each SSE client uses its own connection to the MPD server when using the "idle" command.
This PR refactors the event system using a pub/sub mechanism:

  • Only one goroutine maintains an "idle" connection with the MPD socket.
  • The goroutine receives server updates and publishes them on a channel-based pub/sub topic
  • Any open SSE clients will subscribe to the event topic and receive any published events.

Frontend Changes

  • Updated the event source object to handle the refactored event system
  • New events for mpd connecting/disconnecting
  • New connection status bubbles on the Settings page

@aspage2 aspage2 merged commit 1fafcda into master Mar 21, 2025
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.

2 participants