Merge pull request #1285 from samalba/fix-lock

state: moved stdlib lock to upper function to fix race condition
This commit is contained in:
Sam Alba 2021-12-21 16:33:23 -08:00 committed by GitHub
commit 3e5ce6df26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 20 deletions

View File

@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"github.com/gofrs/flock"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/keychain"
"go.dagger.io/dagger/plancontext"
@ -31,6 +32,7 @@ const (
planDir = "plan"
manifestFile = "values.yaml"
computedFile = "computed.json"
lockFilePath = "dagger.lock"
)
type Project struct {
@ -357,12 +359,6 @@ func cueModInit(ctx context.Context, parentDir string) error {
lg := log.Ctx(ctx)
modDir := path.Join(parentDir, "cue.mod")
if err := os.Mkdir(modDir, 0755); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
}
modFile := path.Join(modDir, "module.cue")
if _, err := os.Stat(modFile); err != nil {
if !errors.Is(err, os.ErrNotExist) {
@ -395,6 +391,28 @@ func VendorUniverse(ctx context.Context, p string) error {
p = getCueModParent()
}
cueModDir := path.Join(p, "cue.mod")
if err := os.Mkdir(cueModDir, 0755); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
}
if err := os.MkdirAll(cueModDir, 0755); err != nil {
return err
}
lockFilePath := path.Join(cueModDir, lockFilePath)
fileLock := flock.New(lockFilePath)
if err := fileLock.Lock(); err != nil {
return err
}
defer func() {
fileLock.Unlock()
os.Remove(lockFilePath)
}()
// ensure cue module is initialized
if err := cueModInit(ctx, p); err != nil {
return err

View File

@ -8,8 +8,6 @@ import (
"os"
"path"
"path/filepath"
"github.com/gofrs/flock"
)
var (
@ -20,21 +18,9 @@ var (
ModuleName = "alpha.dagger.io"
EnginePackage = fmt.Sprintf("%s/europa/dagger/engine", ModuleName)
Path = path.Join("cue.mod", "pkg", ModuleName)
lockFilePath = path.Join("cue.mod", "dagger.lock")
)
func Vendor(ctx context.Context, mod string) error {
lockFilePath := path.Join(mod, lockFilePath)
fileLock := flock.New(lockFilePath)
if err := fileLock.Lock(); err != nil {
return err
}
defer func() {
fileLock.Unlock()
os.Remove(lockFilePath)
}()
// Remove any existing copy of the universe
if err := os.RemoveAll(path.Join(mod, Path)); err != nil {
return err