vishen / go-chromecast

cli for Google Chromecast, Home devices and Cast Groups
Apache License 2.0
853 stars 84 forks source link

fix: Ensure channels are only closed once #171

Closed gabe565 closed 1 year ago

gabe565 commented 1 year ago

Ever since #166, application.Close() will panic with close of closed channel if closed more than once. This PR wraps the close call with sync.Once.Do() so that it can only be called once. This will cause the channel close to simply be skipped after the first call.

This could also be done with a non-blocking select, but has the downside of skipping the close if one of the channels happens to have data at the time that close is called. Let me know if you prefer this other method:

defer func() {
  select {
    case <-a.messageChan:
      // chan probably closed
    default:
      // chan not closed
      close(a.messageChan)
  }
}()
vishen commented 1 year ago

Ah I didn't even think of this use-case. I don't think the non-blocking is the way to go either. Happy with this approach :)

gabe565 commented 1 year ago

Yeah I missed it too until I started testing some connection issues in CastSponsorSkip 😅 Thanks!