浏览代码

llm: prevent race appending to slice (#3320)

Blake Mizerany 1 年之前
父节点
当前提交
acfa2b9422
共有 1 个文件被更改,包括 6 次插入2 次删除
  1. 6 2
      llm/payload_common.go

+ 6 - 2
llm/payload_common.go

@@ -11,6 +11,7 @@ import (
 	"path/filepath"
 	"path/filepath"
 	"runtime"
 	"runtime"
 	"strings"
 	"strings"
+	"sync"
 
 
 	"golang.org/x/exp/slices"
 	"golang.org/x/exp/slices"
 	"golang.org/x/sync/errgroup"
 	"golang.org/x/sync/errgroup"
@@ -147,9 +148,10 @@ func extractDynamicLibs(payloadsDir, glob string) ([]string, error) {
 	if err != nil || len(files) == 0 {
 	if err != nil || len(files) == 0 {
 		return nil, payloadMissing
 		return nil, payloadMissing
 	}
 	}
-	libs := []string{}
 
 
-	g := new(errgroup.Group)
+	var mu sync.Mutex
+	var libs []string
+	var g errgroup.Group
 	for _, file := range files {
 	for _, file := range files {
 		pathComps := strings.Split(file, "/")
 		pathComps := strings.Split(file, "/")
 		if len(pathComps) != pathComponentCount {
 		if len(pathComps) != pathComponentCount {
@@ -182,7 +184,9 @@ func extractDynamicLibs(payloadsDir, glob string) ([]string, error) {
 
 
 			destFile := filepath.Join(targetDir, filepath.Base(filename))
 			destFile := filepath.Join(targetDir, filepath.Base(filename))
 			if strings.Contains(destFile, "server") {
 			if strings.Contains(destFile, "server") {
+				mu.Lock()
 				libs = append(libs, destFile)
 				libs = append(libs, destFile)
+				mu.Unlock()
 			}
 			}
 
 
 			destFp, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
 			destFp, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)