Skip to content

Commit

Permalink
Fix panic for concurrent streams map read and map write #1612
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Feb 24, 2025
1 parent e55c2e9 commit 90544ba
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
8 changes: 4 additions & 4 deletions internal/homekit/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func apiUnpair(id string) error {
return errors.New(api.StreamNotFound)
}

rawURL := findHomeKitURL(stream)
rawURL := findHomeKitURL(stream.Sources())
if rawURL == "" {
return errors.New("not homekit source")
}
Expand All @@ -128,10 +128,10 @@ func apiUnpair(id string) error {

func findHomeKitURLs() map[string]*url.URL {
urls := map[string]*url.URL{}
for id, stream := range streams.Streams() {
if rawURL := findHomeKitURL(stream); rawURL != "" {
for name, sources := range streams.GetAllSources() {
if rawURL := findHomeKitURL(sources); rawURL != "" {
if u, err := url.Parse(rawURL); err == nil {
urls[id] = u
urls[name] = u
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions internal/homekit/homekit.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Init() {
Handler: homekit.ServerHandler(srv),
}

if url := findHomeKitURL(stream); url != "" {
if url := findHomeKitURL(stream.Sources()); url != "" {
// 1. Act as transparent proxy for HomeKit camera
dial := func() (net.Conn, error) {
client, err := homekit.Dial(url, srtp.Server)
Expand Down Expand Up @@ -186,8 +186,7 @@ func hapPairVerify(w http.ResponseWriter, r *http.Request) {
}
}

func findHomeKitURL(stream *streams.Stream) string {
sources := stream.Sources()
func findHomeKitURL(sources []string) string {
if len(sources) == 0 {
return ""
}
Expand Down
4 changes: 2 additions & 2 deletions internal/onvif/onvif.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func onvifDeviceService(w http.ResponseWriter, r *http.Request) {
})

case onvif.MediaGetVideoSources:
b = onvif.GetVideoSourcesResponse(streams.GetAll())
b = onvif.GetVideoSourcesResponse(streams.GetAllNames())

case onvif.MediaGetProfiles:
// important for Hass: H264 codec, width, height
b = onvif.GetProfilesResponse(streams.GetAll())
b = onvif.GetProfilesResponse(streams.GetAllNames())

case onvif.MediaGetProfile:
token := onvif.FindTagValue(b, "ProfileToken")
Expand Down
5 changes: 3 additions & 2 deletions internal/streams/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func NewStream(source any) *Stream {
}
}

func (s *Stream) Sources() (sources []string) {
func (s *Stream) Sources() []string {
sources := make([]string, 0, len(s.producers))
for _, prod := range s.producers {
sources = append(sources, prod.url)
}
return
return sources
}

func (s *Stream) SetSource(source string) {
Expand Down
51 changes: 34 additions & 17 deletions internal/streams/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func Init() {
})
}

func Get(name string) *Stream {
return streams[name]
}

var sanitize = regexp.MustCompile(`\s`)

// Validate - not allow creating dynamic streams with spaces in the source
Expand All @@ -68,6 +64,7 @@ func New(name string, sources ...string) *Stream {
streamsMu.Lock()
streams[name] = stream
streamsMu.Unlock()

return stream
}

Expand Down Expand Up @@ -124,7 +121,7 @@ func GetOrPatch(query url.Values) *Stream {
}

// check if src is stream name
if stream, ok := streams[source]; ok {
if stream := Get(source); stream != nil {
return stream
}

Expand All @@ -139,21 +136,41 @@ func GetOrPatch(query url.Values) *Stream {
return Patch(source, source)
}

func GetAll() (names []string) {
for name := range streams {
names = append(names, name)
}
return
var log zerolog.Logger

// streams map

var streams = map[string]*Stream{}
var streamsMu sync.Mutex

func Get(name string) *Stream {
streamsMu.Lock()
defer streamsMu.Unlock()
return streams[name]
}

func Streams() map[string]*Stream {
return streams
func Delete(name string) {
streamsMu.Lock()
defer streamsMu.Unlock()
delete(streams, name)
}

func Delete(id string) {
delete(streams, id)
func GetAllNames() []string {
streamsMu.Lock()
names := make([]string, 0, len(streams))
for name := range streams {
names = append(names, name)
}
streamsMu.Unlock()
return names
}

var log zerolog.Logger
var streams = map[string]*Stream{}
var streamsMu sync.Mutex
func GetAllSources() map[string][]string {
streamsMu.Lock()
sources := make(map[string][]string, len(streams))
for name, stream := range streams {
sources[name] = stream.Sources()
}
streamsMu.Unlock()
return sources
}

0 comments on commit 90544ba

Please sign in to comment.