Unverified Commit 52a3cdb8 authored by Matthew Fisher's avatar Matthew Fisher Committed by GitHub

Merge pull request #6694 from bacongobbler/fix-6682-helm2

fix(cmd): acquire file lock on repository.lock
parents 07c17133 0a2d5845
...@@ -20,6 +20,8 @@ import ( ...@@ -20,6 +20,8 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"path/filepath"
"strings"
"syscall" "syscall"
"time" "time"
...@@ -135,8 +137,10 @@ func addRepository(name, url, username, password string, home helmpath.Home, cer ...@@ -135,8 +137,10 @@ func addRepository(name, url, username, password string, home helmpath.Home, cer
return fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", url, err.Error()) return fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", url, err.Error())
} }
// Lock the repository file for concurrent goroutines or processes synchronization repoFile := home.RepositoryFile()
fileLock := flock.New(home.RepositoryFile())
// Acquire a file lock for process synchronization
fileLock := flock.New(strings.Replace(repoFile, filepath.Ext(repoFile), ".lock", 1))
lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
locked, err := fileLock.TryLockContext(lockCtx, time.Second) locked, err := fileLock.TryLockContext(lockCtx, time.Second)
...@@ -149,12 +153,12 @@ func addRepository(name, url, username, password string, home helmpath.Home, cer ...@@ -149,12 +153,12 @@ func addRepository(name, url, username, password string, home helmpath.Home, cer
// Re-read the repositories file before updating it as its content may have been changed // Re-read the repositories file before updating it as its content may have been changed
// by a concurrent execution after the first read and before being locked // by a concurrent execution after the first read and before being locked
f, err = repo.LoadRepositoriesFile(home.RepositoryFile()) f, err = repo.LoadRepositoriesFile(repoFile)
if err != nil { if err != nil {
return err return err
} }
f.Update(&c) f.Update(&c)
return f.WriteFile(home.RepositoryFile(), 0644) return f.WriteFile(repoFile, 0644)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment