Skip to content

Commit 95000f9

Browse files
authored
fix data race in GetWebAssets (grafana#90939)
1 parent 0a870e6 commit 95000f9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

pkg/api/webassets/webassets.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11+
"sync"
1112

1213
"github.com/grafana/grafana/pkg/api/dtos"
1314
"github.com/grafana/grafana/pkg/services/licensing"
@@ -31,12 +32,21 @@ type EntryPointInfo struct {
3132
} `json:"assets,omitempty"`
3233
}
3334

34-
var entryPointAssetsCache *dtos.EntryPointAssets = nil
35+
var (
36+
entryPointAssetsCacheMu sync.RWMutex // guard entryPointAssetsCache
37+
entryPointAssetsCache *dtos.EntryPointAssets // TODO: get rid of global state
38+
)
3539

3640
func GetWebAssets(ctx context.Context, cfg *setting.Cfg, license licensing.Licensing) (*dtos.EntryPointAssets, error) {
37-
if cfg.Env != setting.Dev && entryPointAssetsCache != nil {
38-
return entryPointAssetsCache, nil
41+
entryPointAssetsCacheMu.RLock()
42+
ret := entryPointAssetsCache
43+
entryPointAssetsCacheMu.RUnlock()
44+
45+
if cfg.Env != setting.Dev && ret != nil {
46+
return ret, nil
3947
}
48+
entryPointAssetsCacheMu.Lock()
49+
defer entryPointAssetsCacheMu.Unlock()
4050

4151
var err error
4252
var result *dtos.EntryPointAssets

0 commit comments

Comments
 (0)