From 49c9b1aa9a9f5c9754e35d7c90bfca6ec67b59a8 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Fri, 15 Nov 2019 05:27:13 +1100 Subject: [PATCH] Fix race condition in running encoders (#199) --- pkg/ffmpeg/encoder.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/ffmpeg/encoder.go b/pkg/ffmpeg/encoder.go index 405fc317a..7ceb4b594 100644 --- a/pkg/ffmpeg/encoder.go +++ b/pkg/ffmpeg/encoder.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "strings" + "sync" "time" "github.com/stashapp/stash/pkg/logger" @@ -16,7 +17,10 @@ type Encoder struct { Path string } -var runningEncoders map[string][]*os.Process = make(map[string][]*os.Process) +var ( + runningEncoders map[string][]*os.Process = make(map[string][]*os.Process) + runningEncodersMutex = sync.RWMutex{} +) func NewEncoder(ffmpegPath string) Encoder { return Encoder{ @@ -25,12 +29,16 @@ func NewEncoder(ffmpegPath string) Encoder { } func registerRunningEncoder(path string, process *os.Process) { + runningEncodersMutex.Lock() processes := runningEncoders[path] runningEncoders[path] = append(processes, process) + runningEncodersMutex.Unlock() } func deregisterRunningEncoder(path string, process *os.Process) { + runningEncodersMutex.Lock() + defer runningEncodersMutex.Unlock() processes := runningEncoders[path] for i, v := range processes { @@ -49,7 +57,9 @@ func waitAndDeregister(path string, cmd *exec.Cmd) error { } func KillRunningEncoders(path string) { + runningEncodersMutex.RLock() processes := runningEncoders[path] + runningEncodersMutex.RUnlock() for _, process := range processes { // assume it worked, don't check for error