Skip to content

Commit

Permalink
builder: protect LoadInstance with a mutex
Browse files Browse the repository at this point in the history
CUE is not safe for concurrent access so we protect the main
LoadInstance function with a mutex lock.
  • Loading branch information
jeffmccune committed Jan 3, 2025
1 parent 449df91 commit 8660826
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions internal/builder/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
Expand All @@ -19,6 +20,9 @@ import (
"github.com/holos-run/holos/internal/util"
)

// cue context and loading is not safe for concurrent use.
var cueMutex sync.Mutex

// ExtractYAML extracts yaml encoded data from file paths. The data is unified
// into one [cue.Value]. If a path element is a directory, all files in the
// directory are loaded non-recursively.
Expand Down Expand Up @@ -67,6 +71,8 @@ func ExtractYAML(ctxt *cue.Context, filepaths []string) (cue.Value, error) {
// extracted data values are unified with the platform configuration [cue.Value]
// in the returned [Instance].
func LoadInstance(path string, filepaths []string, tags []string) (*Instance, error) {
cueMutex.Lock()
defer cueMutex.Unlock()
root, leaf, err := util.FindRootLeaf(path)
if err != nil {
return nil, errors.Wrap(err)
Expand Down
2 changes: 1 addition & 1 deletion version/embedded/patch
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2
3

0 comments on commit 8660826

Please sign in to comment.