Skip to content

Automated Test: asset-loading-optimized #331

Closed

Conversation

admin-coderabbit
Copy link
Owner

@admin-coderabbit admin-coderabbit commented Feb 4, 2026

This pull request was automatically created by @coderabbitai/e2e-reviewer.

Batch created pull request.

Summary by CodeRabbit

  • Chores
    • Improved web assets caching efficiency and reliability through enhanced internal concurrency handling.

@coderabbit-eval
Copy link

coderabbit-eval bot commented Feb 4, 2026

📝 Walkthrough

Walkthrough

The change introduces thread-safe caching for web assets in the webassets package by adding a RWMutex and cache variable. Read locks retrieve cached assets in non-development environments, while write locks guard cache population and updates. Asset loading behavior remains unchanged.

Changes

Cohort / File(s) Summary
Web Assets Cache Synchronization
pkg/api/webassets/webassets.go
Added RWMutex (entryPointAssetsCacheMu) and cache variable (entryPointAssetsCache) to protect concurrent access. Replaced nil-check with read-lock for cache retrieval and added write-lock for cache updates.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Poem

🐰 With locks so fine, no race we'll find,
Our assets cached, thread-safe in mind,
RWMutex guards the sacred store,
Concurrency blessed forevermore!

🚥 Pre-merge checks | ✅ 1 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Automated Test: asset-loading-optimized' is vague and generic, using non-descriptive terms that don't convey the actual technical change (adding concurrency-safe caching with RWMutex to web assets). Revise the title to clearly describe the main change, such as 'Add thread-safe caching to web asset loading' or 'Implement RWMutex for concurrent web asset cache access'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch asset-loading-optimized

Comment @coderabbit eval help to get the list of available commands and usage tips.

@coderabbit-eval coderabbit-eval bot closed this Feb 4, 2026
Copy link

@coderabbit-eval coderabbit-eval bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@pkg/api/webassets/webassets.go`:
- Around line 40-50: GetWebAssets has a TOCTOU: after releasing the read lock
and before loading assets you must re-check the cache under the write lock to
avoid redundant loads; after entryPointAssetsCacheMu.Lock() (inside
GetWebAssets) add a second check of entryPointAssetsCache (and return it if
non-nil) before performing the expensive asset loading logic, keeping the
existing cfg.Env / setting.Dev behavior and using the same entryPointAssetsCache
and entryPointAssetsCacheMu symbols.

Comment on lines 40 to 50
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()

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Missing double-check after acquiring write lock causes redundant asset loading.

The current implementation has a TOCTOU (time-of-check to time-of-use) issue. Between releasing the read lock (line 43) and acquiring the write lock (line 48), another goroutine may have already populated the cache. Without a re-check, multiple concurrent requests at startup will all perform redundant file/CDN reads.

🔧 Proposed fix: Add double-check after acquiring write lock
 func GetWebAssets(ctx context.Context, cfg *setting.Cfg, license licensing.Licensing) (*dtos.EntryPointAssets, error) {
 	entryPointAssetsCacheMu.RLock()
 	ret := entryPointAssetsCache
 	entryPointAssetsCacheMu.RUnlock()

 	if cfg.Env != setting.Dev && ret != nil {
 		return ret, nil
 	}
 	entryPointAssetsCacheMu.Lock()
 	defer entryPointAssetsCacheMu.Unlock()

+	// Double-check after acquiring write lock
+	if cfg.Env != setting.Dev && entryPointAssetsCache != nil {
+		return entryPointAssetsCache, nil
+	}
+
 	var err error
 	var result *dtos.EntryPointAssets
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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()
func GetWebAssets(ctx context.Context, cfg *setting.Cfg, license licensing.Licensing) (*dtos.EntryPointAssets, error) {
entryPointAssetsCacheMu.RLock()
ret := entryPointAssetsCache
entryPointAssetsCacheMu.RUnlock()
if cfg.Env != setting.Dev && ret != nil {
return ret, nil
}
entryPointAssetsCacheMu.Lock()
defer entryPointAssetsCacheMu.Unlock()
// Double-check after acquiring write lock
if cfg.Env != setting.Dev && entryPointAssetsCache != nil {
return entryPointAssetsCache, nil
}
🤖 Prompt for AI Agents
In `@pkg/api/webassets/webassets.go` around lines 40 - 50, GetWebAssets has a
TOCTOU: after releasing the read lock and before loading assets you must
re-check the cache under the write lock to avoid redundant loads; after
entryPointAssetsCacheMu.Lock() (inside GetWebAssets) add a second check of
entryPointAssetsCache (and return it if non-nil) before performing the expensive
asset loading logic, keeping the existing cfg.Env / setting.Dev behavior and
using the same entryPointAssetsCache and entryPointAssetsCacheMu symbols.

Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant