From af2a56a6803a83f3e67ba74851ee8662f8db6832 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Fri, 18 Oct 2024 12:09:27 -0700 Subject: [PATCH] bug: lock when setting up runtimes --- pkg/repos/runtimes/busybox/busybox.go | 5 +++++ pkg/repos/runtimes/golang/golang.go | 6 ++++++ pkg/repos/runtimes/node/node.go | 6 ++++++ pkg/repos/runtimes/python/python.go | 6 ++++++ 4 files changed, 23 insertions(+) diff --git a/pkg/repos/runtimes/busybox/busybox.go b/pkg/repos/runtimes/busybox/busybox.go index e4604b06..5c77ee2b 100644 --- a/pkg/repos/runtimes/busybox/busybox.go +++ b/pkg/repos/runtimes/busybox/busybox.go @@ -14,6 +14,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env" "github.com/gptscript-ai/gptscript/pkg/hash" @@ -27,6 +28,7 @@ var releasesData []byte const downloadURL = "https://github.com/gptscript-ai/busybox-w32/releases/download/%s" type Runtime struct { + runtimeSetupLock sync.Mutex } func (r *Runtime) ID() string { @@ -75,6 +77,9 @@ func (r *Runtime) getReleaseAndDigest() (string, string, error) { } func (r *Runtime) getRuntime(ctx context.Context, cwd string) (string, error) { + r.runtimeSetupLock.Lock() + defer r.runtimeSetupLock.Unlock() + url, sha, err := r.getReleaseAndDigest() if err != nil { return "", err diff --git a/pkg/repos/runtimes/golang/golang.go b/pkg/repos/runtimes/golang/golang.go index f86fa88d..23c12f1a 100644 --- a/pkg/repos/runtimes/golang/golang.go +++ b/pkg/repos/runtimes/golang/golang.go @@ -17,6 +17,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" "github.com/gptscript-ai/gptscript/pkg/config" "github.com/gptscript-ai/gptscript/pkg/debugcmd" @@ -34,6 +35,8 @@ const downloadURL = "https://go.dev/dl/" type Runtime struct { // version something like "1.22.1" Version string + + runtimeSetupLock sync.Mutex } func (r *Runtime) ID() string { @@ -355,6 +358,9 @@ func (r *Runtime) binDir(rel string) string { } func (r *Runtime) getRuntime(ctx context.Context, cwd string) (string, error) { + r.runtimeSetupLock.Lock() + defer r.runtimeSetupLock.Unlock() + url, sha, err := r.getReleaseAndDigest() if err != nil { return "", err diff --git a/pkg/repos/runtimes/node/node.go b/pkg/repos/runtimes/node/node.go index 4d73c13b..53b77ca2 100644 --- a/pkg/repos/runtimes/node/node.go +++ b/pkg/repos/runtimes/node/node.go @@ -12,6 +12,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" "github.com/gptscript-ai/gptscript/pkg/debugcmd" runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env" @@ -34,6 +35,8 @@ type Runtime struct { Version string // If true this is the version that will be used for python or python3 Default bool + + runtimeSetupLock sync.Mutex } func (r *Runtime) ID() string { @@ -175,6 +178,9 @@ func (r *Runtime) binDir(rel string) (string, error) { } func (r *Runtime) getRuntime(ctx context.Context, cwd string) (string, error) { + r.runtimeSetupLock.Lock() + defer r.runtimeSetupLock.Unlock() + url, sha, err := r.getReleaseAndDigest() if err != nil { return "", err diff --git a/pkg/repos/runtimes/python/python.go b/pkg/repos/runtimes/python/python.go index ee4bf571..4aebe0cf 100644 --- a/pkg/repos/runtimes/python/python.go +++ b/pkg/repos/runtimes/python/python.go @@ -12,6 +12,7 @@ import ( "os" "path/filepath" "runtime" + "sync" "github.com/gptscript-ai/gptscript/pkg/debugcmd" runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env" @@ -42,6 +43,8 @@ type Runtime struct { Version string // If true this is the version that will be used for python or python3 Default bool + + runtimeSetupLock sync.Mutex } func (r *Runtime) ID() string { @@ -234,6 +237,9 @@ func (r *Runtime) setupUV(ctx context.Context, tmp string) error { } func (r *Runtime) getRuntime(ctx context.Context, cwd string) (string, error) { + r.runtimeSetupLock.Lock() + defer r.runtimeSetupLock.Unlock() + url, sha, err := r.getReleaseAndDigest() if err != nil { return "", err