Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

The Manager.openSdks map was accessed concurrently without synchronization, causing fatal panics when activate.go spawns multiple goroutines calling LookupSdk() in parallel.

Changes

  • Added sync.RWMutex to Manager struct to protect map access

    • Read operations (LookupSdk cache lookups) use RLock for concurrent reads
    • Write operations (LookupSdk, LoadAllSdk) use Lock for exclusive access
    • Close() uses Lock to prevent access during cleanup
  • Fixed cache key inconsistency where reads used original name but writes used strings.ToLower(name), causing cache misses

Example

// Before: concurrent map writes panic
for sdkName, version := range allTools {
    g.Go(func() error {
        sdkObj, err := manager.LookupSdk(sdkName)  // ⚠️ Unsafe concurrent access
        // ...
    })
}

// After: thread-safe with RWMutex
func (m *Manager) LookupSdk(name string) (sdk.Sdk, error) {
    normalizedName := strings.ToLower(name)
    
    m.mu.RLock()  // ✓ Concurrent reads OK
    s, ok := m.openSdks[normalizedName]
    m.mu.RUnlock()
    // ...
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • version-fox.github.io
    • Triggering command: /tmp/go-build1703830412/b355/http.test /tmp/go-build1703830412/b355/http.test -test.testlogfile=/tmp/go-build1703830412/b355/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build286749925/b156/vet.cfg x64/src/runtime/go1.24.11 4160351/b189/vet-symabis ache/go/1.24.11//tmp/go-build286749925/b252/symabis 0Y9pJDtq_ --64 -o 2.o -o x64/src/runtime/cgo -trimpath x64/pkg/tool/linux_amd64/compile--64 -p main -lang=go1.24 x64/pkg/tool/lin-buildtags (dns block)
    • Triggering command: /tmp/go-build2841787517/b371/http.test /tmp/go-build2841787517/b371/http.test -test.testlogfile=/tmp/go-build2841787517/b371/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.short=true 749925/b192/_pkg-p -buildtags ux_amd64/compile-lang=go1.24 -errorsas g/x/crypto/inter-o -nilfunc ux_amd64/compile-trimpath -uns�� x64/src/os/user /tmp/go-build141github.com/version-fox/vfox/internal/shared/printer ux_amd64/vet jqT2/5HkHinSjDRibash me.go (dns block)
    • Triggering command: /tmp/go-build1072179428/b355/http.test /tmp/go-build1072179428/b355/http.test -test.testlogfile=/tmp/go-build1072179428/b355/testlog.txt -test.paniconexit0 -test.timeout=10m0s o_.o�� x64/src/net -trimpath ux_amd64/vet -p github.com/shirols-files -lang=go1.23 ux_amd64/vet ter-�� ache/go/1.24.11/x64/src/os/user cfg x64/pkg/tool/linux_amd64/vet SR_6/YDHLU3HiOzV/bin/sh ions =0 x64/pkg/tool/linux_amd64/vet (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

fatal error: concurrent map writes

goroutine 7 [running]:
internal/runtime/maps.fatal({0x10154a30b?, 0x1014db958?})
runtime/panic.go:1053 +0x20
github.com/version-fox/vfox/internal.(*Manager).LookupSdk(0x140001f5ce0, {0x1400002ce78, 0x5})
github.com/version-fox/vfox/internal/manager.go:98 +0x258
github.com/version-fox/vfox/cmd/commands.activateCmd.func2()
github.com/version-fox/vfox/cmd/commands/activate.go:94 +0x60
golang.org/x/sync/errgroup.(*Group).Go.func1()
golang.org/x/sync@v0.19.0/errgroup/errgroup.go:93 +0x54
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
golang.org/x/sync@v0.19.0/errgroup/errgroup.go:78 +0x94

goroutine 1 [sync.WaitGroup.Wait]:
sync.runtime_SemacquireWaitGroup(0x1400003b2d8?)
runtime/sema.go:110 +0x2c
sync.(*WaitGroup).Wait(0x14000043f48)
sync/waitgroup.go:118 +0x70
golang.org/x/sync/errgroup.(*Group).Wait(0x14000043f40)
golang.org/x/sync@v0.19.0/errgroup/errgroup.go:56 +0x28
github.com/version-fox/vfox/cmd/commands.activateCmd({0x10176f960, 0x140001e7e00}, 0x101c18dc0)
github.com/version-fox/vfox/cmd/commands/activate.go:141 +0x52c
github.com/urfave/cli/v3.(*Command).run(0x101c18dc0, {0x10176f960, 0x140001e7e00}, {0x1400007da40, 0x2, 0x2})
github.com/urfave/cli/v3@v3.4.1/command_run.go:344 +0x1cfc
github.com/urfave/cli/v3.(*Command).run(0x140001c0288, {0x10176f960, 0x140001e6f30}, {0x140000201b0, 0x3, 0x3})
github.com/urfave/cli/v3@v3.4.1/command_run.go:278 +0x1744
github.com/urfave/cli/v3.(*Command).Run(...)
github.com/urfave/cli/v3@v3.4.1/command_run.go:94
github.com/version-fox/vfox/cmd.(*cmd).Execute(0x0?, {0x140000201b0?, 0x14000064738?, 0x0?})
github.com/version-fox/vfox/cmd/cmd.go:40 +0x40
github.com/version-fox/vfox/cmd.Execute({0x140000201b0, 0x3, 0x3})
github.com/version-fox/vfox/cmd/cmd.go:31 +0x38
main.main()
github.com/version-fox/vfox/main.go:26 +0x34

goroutine 8 [runnable]:
syscall.syscall(0x0?, 0x30?, 0x0?, 0x0?)
runtime/sys_darwin.go:27 +0x48
syscall.closedir(0x1400025ba08?)
syscall/zsyscall_darwin_arm64.go:559 +0x34
os.(*dirInfo).close(...)
os/dir_darwin.go:23
os.(*file).close(0x14000110060)
os/file_unix.go:330 +0x48
os.(*File).Close(...)
os/file_posix.go:23
os.ReadDir({0x1400002d0b0?, 0x101539f43?})
os/dir.go:129 +0x124
github.com/version-fox/vfox/internal/sdk.(*impl).GetRuntimePackage(0x140003d9d40, {0x1400002ce92, 0x6})
github.com/version-fox/vfox/internal/sdk/sdk.go:818 +0xe4
github.com/version-fox/vfox/internal/sdk.(*impl).CreateSymlinksForScope(0x140003d9d40, {0x1400002ce92?, 0x6?}, 0x0)
github.com/version-fox/vfox/internal/sdk/sdk.go:739 +0x28
github.com/version-fox/vfox/cmd/commands.activateCmd.func2()
github.com/version-fox/vfox/cmd/commands/activate.go:118 +0x26c
golang.org/x/sync/errgroup.(*Group).Go.func1()
golang.org/x/sync@v0.19.0/errgroup/errgroup.go:93 +0x54
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
golang.org/x/sync@v0.19.0/errgroup/errgroup.go:78 +0x94


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits January 16, 2026 07:33
Co-authored-by: aooohan <40265686+aooohan@users.noreply.github.com>
Co-authored-by: aooohan <40265686+aooohan@users.noreply.github.com>
…ocking

Co-authored-by: aooohan <40265686+aooohan@users.noreply.github.com>
Co-authored-by: aooohan <40265686+aooohan@users.noreply.github.com>
Co-authored-by: aooohan <40265686+aooohan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix concurrent map writes error in Manager lookup Fix concurrent map writes in Manager.LookupSdk Jan 16, 2026
Copilot AI requested a review from aooohan January 16, 2026 07:41
@aooohan aooohan marked this pull request as ready for review January 16, 2026 08:15
Copilot AI review requested due to automatic review settings January 16, 2026 08:15
@codecov
Copy link

codecov bot commented Jan 16, 2026

Codecov Report

❌ Patch coverage is 53.33333% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 22.35%. Comparing base (799c95a) to head (1670724).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/manager.go 53.33% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #610      +/-   ##
==========================================
+ Coverage   21.94%   22.35%   +0.40%     
==========================================
  Files          80       80              
  Lines        5376     5387      +11     
==========================================
+ Hits         1180     1204      +24     
+ Misses       4065     4047      -18     
- Partials      131      136       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical concurrent map access bug in Manager.LookupSdk that was causing fatal panics when multiple goroutines called the method simultaneously (as occurs in activate.go). The fix adds thread-safe synchronization using sync.RWMutex and corrects a cache key inconsistency.

Changes:

  • Added sync.RWMutex to the Manager struct to synchronize access to the openSdks map
  • Fixed cache key normalization inconsistency where reads used the original name but writes used strings.ToLower(name)
  • Added comprehensive concurrency tests covering multiple scenarios including concurrent lookups, cache hits, and the actual activate.go use case

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
internal/manager.go Added RWMutex field and synchronized all map access operations in LookupSdk, LoadAllSdk, and Close methods
internal/manager_test.go Added 5 new test functions to verify thread-safe concurrent access to the SDK manager

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +109 to +111
m.mu.Lock()
m.openSdks[normalizedName] = s
m.mu.Unlock()
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

There's a potential race condition where two goroutines could both pass the cache check (lines 83-85), then both load the SDK (line 102), and both attempt to cache it. While this won't cause a panic with the mutex, it creates unnecessary duplicate work and could lead to resource leaks if sdk.NewSdk allocates resources. Consider implementing a double-check lock pattern: after acquiring the write lock, check if another goroutine has already cached the SDK, and if so, close the newly created SDK and return the cached one instead.

Copilot uses AI. Check for mistakes.
Comment on lines +81 to +83
if err == nil {
t.Errorf("Expected error for non-existent SDK, got nil")
}
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The test uses t.Errorf inside a goroutine, which is unsafe and can cause races on the testing.T object. Use t.Error or better yet, collect errors in a channel and check them after wg.Wait(), or use a mutex-protected slice to collect error messages.

Copilot uses AI. Check for mistakes.
// We expect errors since these SDKs don't exist
// But we should not panic or race
if err == nil {
t.Errorf("Expected error for non-existent SDK %s, got nil", sdkName)
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The test uses t.Errorf inside a goroutine, which is unsafe and can cause races on the testing.T object. Use t.Error or better yet, collect errors in a channel and check them after wg.Wait(), or use a mutex-protected slice to collect error messages.

Copilot uses AI. Check for mistakes.
@aooohan aooohan merged commit bc5edee into main Jan 16, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants