Skip to content

Commit

Permalink
fix: race in sideload update (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
docmerlin authored Apr 14, 2021
1 parent 1bdb883 commit c5603f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions services/sideload/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func (s *fileSource) Close() {
}

func (s *fileSource) UpdateCache() error {
s.mu.Lock()
defer s.mu.Unlock()
s.cache = make(map[string]map[string]interface{})
err := filepath.Walk(s.dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -258,7 +260,10 @@ type httpSource struct {
}

func (s *httpSource) UpdateCache() error {
s.cache = make(map[string]map[string]interface{})
s.fileSource.mu.Lock()
defer s.fileSource.mu.Unlock()
s.fileSource.cache = make(map[string]map[string]interface{})

req, err := http.NewRequest("GET", s.dir, nil)
if err != nil {
return errors.Wrapf(err, "failed to generate request to update sideload cache for source %q", s.dir)
Expand All @@ -281,7 +286,7 @@ func (s *httpSource) UpdateCache() error {
return errors.Wrapf(err, "failed to load body to update sideload cache for source %q", s.dir)
}
for k, v := range values {
s.cache[k] = v
s.fileSource.cache[k] = v
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions services/sideload/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ func TestService_Source_Lookup(t *testing.T) {
for i, tc := range testCases {
tc := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
got := src.Lookup(tc.order, tc.key)
if !cmp.Equal(got, tc.want) {
t.Errorf("unexpected values: -want/+got:\n%s", cmp.Diff(tc.want, got))
}
if err := e.Update(conf); err != nil {
t.Fatal(err)
}
})
}
}

0 comments on commit c5603f3

Please sign in to comment.