Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/api/webassets/webassets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"sync"

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

var entryPointAssetsCache *dtos.EntryPointAssets = nil
var (
entryPointAssetsCacheMu sync.RWMutex // guard entryPointAssetsCache
entryPointAssetsCache *dtos.EntryPointAssets // TODO: get rid of global state
)

func GetWebAssets(ctx context.Context, cfg *setting.Cfg, license licensing.Licensing) (*dtos.EntryPointAssets, error) {
if cfg.Env != setting.Dev && entryPointAssetsCache != nil {
return entryPointAssetsCache, nil
entryPointAssetsCacheMu.RLock()
ret := entryPointAssetsCache
entryPointAssetsCacheMu.RUnlock()

if cfg.Env != setting.Dev && ret != nil {
return ret, nil
}
entryPointAssetsCacheMu.Lock()
defer entryPointAssetsCacheMu.Unlock()
Comment on lines +48 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: missing double-check after acquiring write lock - multiple goroutines can populate cache simultaneously

Suggested change
entryPointAssetsCacheMu.Lock()
defer entryPointAssetsCacheMu.Unlock()
entryPointAssetsCacheMu.Lock()
defer entryPointAssetsCacheMu.Unlock()
// Re-check after acquiring write lock
if cfg.Env != setting.Dev && entryPointAssetsCache != nil {
return entryPointAssetsCache, nil
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/api/webassets/webassets.go
Line: 48:49

Comment:
**logic:** missing double-check after acquiring write lock - multiple goroutines can populate cache simultaneously

```suggestion
	entryPointAssetsCacheMu.Lock()
	defer entryPointAssetsCacheMu.Unlock()
	
	// Re-check after acquiring write lock
	if cfg.Env != setting.Dev && entryPointAssetsCache != nil {
		return entryPointAssetsCache, nil
	}
```

How can I resolve this? If you propose a fix, please make it concise.


var err error
var result *dtos.EntryPointAssets
Expand Down
Loading