From acfa2b94220882ffe8246a56e35a6cd1db38e6a2 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Sun, 24 Mar 2024 11:35:54 -0700 Subject: [PATCH] llm: prevent race appending to slice (#3320) --- llm/payload_common.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/llm/payload_common.go b/llm/payload_common.go index 5c4db622..d45b70c4 100644 --- a/llm/payload_common.go +++ b/llm/payload_common.go @@ -11,6 +11,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" "golang.org/x/exp/slices" "golang.org/x/sync/errgroup" @@ -147,9 +148,10 @@ func extractDynamicLibs(payloadsDir, glob string) ([]string, error) { if err != nil || len(files) == 0 { return nil, payloadMissing } - libs := []string{} - g := new(errgroup.Group) + var mu sync.Mutex + var libs []string + var g errgroup.Group for _, file := range files { pathComps := strings.Split(file, "/") if len(pathComps) != pathComponentCount { @@ -182,7 +184,9 @@ func extractDynamicLibs(payloadsDir, glob string) ([]string, error) { destFile := filepath.Join(targetDir, filepath.Base(filename)) if strings.Contains(destFile, "server") { + mu.Lock() libs = append(libs, destFile) + mu.Unlock() } destFp, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)