From 7416a8237d469e97e00edb9bdfe063c46a1f8330 Mon Sep 17 00:00:00 2001 From: Stash Dev Date: Thu, 7 Nov 2019 11:12:40 -0800 Subject: [PATCH] Fixed a race condition in running_streams.go --- pkg/manager/running_streams.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/manager/running_streams.go b/pkg/manager/running_streams.go index f573d7aa4..c8787387b 100644 --- a/pkg/manager/running_streams.go +++ b/pkg/manager/running_streams.go @@ -2,20 +2,27 @@ package manager import ( "net/http" + "sync" "github.com/stashapp/stash/pkg/ffmpeg" "github.com/stashapp/stash/pkg/logger" ) -var streamingFiles = make(map[string][]*http.ResponseWriter) +var ( + streamingFiles = make(map[string][]*http.ResponseWriter) + streamingFilesMutex = sync.RWMutex{} +) func RegisterStream(filepath string, w *http.ResponseWriter) { + streamingFilesMutex.Lock() streams := streamingFiles[filepath] - streamingFiles[filepath] = append(streams, w) + streamingFilesMutex.Unlock() } func deregisterStream(filepath string, w *http.ResponseWriter) { + streamingFilesMutex.Lock() + defer streamingFilesMutex.Unlock() streams := streamingFiles[filepath] for i, v := range streams { @@ -37,7 +44,9 @@ func WaitAndDeregisterStream(filepath string, w *http.ResponseWriter, r *http.Re func KillRunningStreams(path string) { ffmpeg.KillRunningEncoders(path) + streamingFilesMutex.RLock() streams := streamingFiles[path] + streamingFilesMutex.RUnlock() for _, w := range streams { hj, ok := (*w).(http.Hijacker)