From ce935525db5f4caedd6a512d782ac7ba6b4fd1e9 Mon Sep 17 00:00:00 2001 From: Mickael Kerjean Date: Mon, 10 Oct 2022 08:22:45 +1100 Subject: [PATCH] fix (config): concurrent issue fatal error: sync: unlock of unlocked mutex goroutine 6 [running]: runtime.throw(0x1420562, 0x1e) /usr/local/go/src/runtime/panic.go:1117 +0x72 fp=0xc0000b5ec8 sp=0xc0000b5e98 pc=0x439e92 sync.throw(0x1420562, 0x1e) /usr/local/go/src/runtime/panic.go:1103 +0x35 fp=0xc0000b5ee8 sp=0xc0000b5ec8 pc=0x46e395 sync.(*Mutex).unlockSlow(0x1f97138, 0xffffffff) /usr/local/go/src/sync/mutex.go:196 +0xd8 fp=0xc0000b5f10 sp=0xc0000b5ee8 pc=0x47d458 sync.(*Mutex).Unlock(...) /usr/local/go/src/sync/mutex.go:190 github.com/mickael-kerjean/filestash/server/common.(*Configuration).Get(0x1f97120, 0x140a506, 0x14, 0x0) /home/mickael/Documents/projects/filestash/server/common/config.go:438 +0x113 fp=0xc0000b5f90 sp=0xc0000b5f10 pc=0x7ba313 github.com/mickael-kerjean/filestash/server/common.init.0.func1(0x0) /home/mickael/Documents/projects/filestash/server/common/api.go:16 +0x46 fp=0xc0000b5fc0 sp=0xc0000b5f90 pc=0x7c7906 github.com/mickael-kerjean/filestash/server/common.init.0.func3() /home/mickael/Documents/projects/filestash/server/common/api.go:40 +0x29 fp=0xc0000b5fe0 sp=0xc0000b5fc0 pc=0x7c7c49 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc0000b5fe8 sp=0xc0000b5fe0 pc=0x4733e1 created by github.com/mickael-kerjean/filestash/server/common.init.0 /home/mickael/Documents/projects/filestash/server/common/api.go:39 +0x5a goroutine 1 [runnable, locked to thread]: os.(*File).Read(0xc000186000, 0xc000280400, 0x100, 0x100, 0x401, 0xc000280000, 0x400) /usr/local/go/src/os/file.go:113 +0x265 io.ReadAll(0x1677e40, 0xc000186000, 0x0, 0x1ff, 0xc000186000, 0x0, 0x0) /usr/local/go/src/io/io.go:633 +0xdf io/ioutil.ReadAll(...) /usr/local/go/src/io/ioutil/ioutil.go:27 github.com/mickael-kerjean/filestash/server/common.LoadConfig(0x0, 0x0, 0x0, 0x1fc66e0, 0x0) /home/mickael/Documents/projects/filestash/server/common/config_state.go:32 +0x85 github.com/mickael-kerjean/filestash/server/common.(*Configuration).Load(0x1f97120) /home/mickael/Documents/projects/filestash/server/common/config.go:214 +0x34 github.com/mickael-kerjean/filestash/server/common.init.1() /home/mickael/Documents/projects/filestash/server/common/config.go:53 +0x85 --- server/common/api.go | 47 ++++++----------------------------------- server/common/config.go | 7 ++++-- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/server/common/api.go b/server/common/api.go index 11a9d7f7..e44fac2d 100644 --- a/server/common/api.go +++ b/server/common/api.go @@ -6,55 +6,22 @@ import ( "strings" ) -var ( - isApiEnabled func() bool - getApiKey func() string -) - -func init() { - isApiEnabled = func() bool { - return Config.Get("features.api.enabled").Schema(func(f *FormElement) *FormElement { - if f == nil { - f = &FormElement{} - } - f.Name = "enabled" - f.Type = "boolean" - f.Description = "Enable/Disable the API" - f.Default = true - return f - }).Bool() - } - getApiKey = func() string { - return Config.Get("features.api.api_key").Schema(func(f *FormElement) *FormElement { - if f == nil { - f = &FormElement{} - } - f.Name = "api_key" - f.Type = "long_text" - f.Description = "Format: '[mandatory:key] [optional:hostname]'. The hostname is used to enabled CORS for your application." - f.Placeholder = "foobar *.filestash.app" - return f - }).String() - } - go func() { - isApiEnabled() - getApiKey() - }() -} - func VerifyApiKey(api_key string) (host string, err error) { - if isApiEnabled() == false { + isApiEnabled := Config.Get("features.api.enable").Bool() + apiKey := Config.Get("feature.api.api_key").String() + + if isApiEnabled == false { return "", NewError("Api is not enabled", 503) - } else if api_key == os.Getenv("API_KEY") { + } else if apiKey == os.Getenv("API_KEY") { return "*", nil } - lines := strings.Split(getApiKey(), "\n") + lines := strings.Split(apiKey, "\n") for _, line := range lines { line = regexp.MustCompile(` #.*`).ReplaceAllString(line, "") // remove comment chunks := strings.SplitN(line, " ", 2) if len(chunks) == 0 { continue - } else if chunks[0] != api_key { + } else if chunks[0] != apiKey { continue } if len(chunks) == 1 { diff --git a/server/common/config.go b/server/common/config.go index 1d6ac1bf..e55491a7 100644 --- a/server/common/config.go +++ b/server/common/config.go @@ -85,8 +85,11 @@ func NewConfiguration() Configuration { Title: "features", Form: []Form{ Form{ - Title: "api", - Elmnts: []FormElement{}, + Title: "api", + Elmnts: []FormElement{ + FormElement{Name: "enable", Type: "boolean", Default: true, Description: "Enable/Disable the API"}, + FormElement{Name: "api_key", Type: "long_text", Description: "Format: '[mandatory:key] [optional:hostname]'. The hostname is used to enabled CORS for your application.", Placeholder: "foobar *.filestash.app"}, + }, }, Form{ Title: "share",