Fixed a race condition when changing the config file

This commit is contained in:
Alex X
2025-09-19 15:26:54 +03:00
parent 3f542a642c
commit 45cbbaf1cf

View File

@@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"github.com/AlexxIT/go2rtc/pkg/shell" "github.com/AlexxIT/go2rtc/pkg/shell"
"github.com/AlexxIT/go2rtc/pkg/yaml" "github.com/AlexxIT/go2rtc/pkg/yaml"
@@ -18,11 +19,16 @@ func LoadConfig(v any) {
} }
} }
var configMu sync.Mutex
func PatchConfig(path []string, value any) error { func PatchConfig(path []string, value any) error {
if ConfigPath == "" { if ConfigPath == "" {
return errors.New("config file disabled") return errors.New("config file disabled")
} }
configMu.Lock()
defer configMu.Unlock()
// empty config is OK // empty config is OK
b, _ := os.ReadFile(ConfigPath) b, _ := os.ReadFile(ConfigPath)