Skip to content

Commit 145cd82

Browse files
committed
pkg/cdi: do GetSpecErrors() with locked cache.
Avoid a read/write data race between refresh() and GetSpecErrors() by taking the cache lock during GetSpecErrors(). While at it, also make a copy and return that instead of the original error slice in the Spec. Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent d4004b3 commit 145cd82

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

pkg/cdi/cache.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,17 @@ func (c *Cache) GetVendorSpecs(vendor string) []*Spec {
409409
// GetSpecErrors returns all errors encountered for the spec during the
410410
// last cache refresh.
411411
func (c *Cache) GetSpecErrors(spec *Spec) []error {
412-
return c.errors[spec.GetPath()]
412+
var errors []error
413+
414+
c.Lock()
415+
defer c.Unlock()
416+
417+
if errs, ok := c.errors[spec.GetPath()]; ok {
418+
errors = make([]error, len(errs))
419+
copy(errors, errs)
420+
}
421+
422+
return errors
413423
}
414424

415425
// GetErrors returns all errors encountered during the last

0 commit comments

Comments
 (0)