mirror of
https://github.com/turbot/steampipe.git
synced 2025-12-19 09:58:53 -05:00
Fix race condition in autoCompleteSuggestions.sort() with mutex
Add mutex synchronization to the autoCompleteSuggestions struct to prevent concurrent modification of slice data during sorting operations. The sort() method now acquires a lock before performing any sorting operations, ensuring that only one goroutine can sort the suggestions at a time. This prevents the data races detected when sort() was called concurrently. Changes: - Add sync.Mutex field to autoCompleteSuggestions struct - Change sort() receiver from value to pointer to support mutex - Add Lock/Unlock calls around sorting operations The test TestAutocompleteSuggestions_ConcurrentSort now passes with the -race flag, and all existing tests continue to pass. Fixes #4711 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package interactive
|
||||
|
||||
import (
|
||||
"github.com/c-bata/go-prompt"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/c-bata/go-prompt"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -15,6 +17,7 @@ const (
|
||||
)
|
||||
|
||||
type autoCompleteSuggestions struct {
|
||||
mu sync.Mutex
|
||||
schemas []prompt.Suggest
|
||||
unqualifiedTables []prompt.Suggest
|
||||
unqualifiedQueries []prompt.Suggest
|
||||
@@ -72,7 +75,10 @@ func (s *autoCompleteSuggestions) setQueriesForMod(modName string, queries []pro
|
||||
s.queriesByMod[modName] = queries
|
||||
}
|
||||
|
||||
func (s autoCompleteSuggestions) sort() {
|
||||
func (s *autoCompleteSuggestions) sort() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
sortSuggestions := func(s []prompt.Suggest) {
|
||||
sort.Slice(s, func(i, j int) bool {
|
||||
return s[i].Text < s[j].Text
|
||||
|
||||
Reference in New Issue
Block a user