Skip to content

pkg/cdi: add missing error checks in cache #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions pkg/cdi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func NewCache(options ...Option) (*Cache, error) {
watch: &watch{},
}

WithSpecDirs(DefaultSpecDirs...)(c)
if err := WithSpecDirs(DefaultSpecDirs...)(c); err != nil {
Copy link
Contributor

@klihub klihub Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithSpecDirs is an Option which can't fail, so this is definitely a behavior-unaltering change and as such definitely fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok, this lead me to submit #187 (replaces this particular change)

return nil, err
}
c.Lock()
defer c.Unlock()

Expand Down Expand Up @@ -107,9 +109,7 @@ func (c *Cache) configure(options ...Option) error {
c.watch.setup(c.specDirs, c.dirErrors)
c.watch.start(&c.Mutex, c.refresh, c.dirErrors)
}
c.refresh()

return nil
return c.refresh()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, off the top of my head I'm not sure if this could cause problems.

Looking at the code, maybe it would be better/simpler/cleaner to change the API so that [Rr]efresh() never returns an error. Since errors encountered during the last refresh can anyway be queried using GetErrors(), if a caller is interested in errors it can query them itself.

}

// Refresh rescans the CDI Spec directories and refreshes the Cache.
Expand Down Expand Up @@ -224,7 +224,7 @@ func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, e
c.Lock()
defer c.Unlock()

c.refreshIfRequired(false)
_, _ = c.refreshIfRequired(false)

edits := &ContainerEdits{}
specs := map[*Spec]struct{}{}
Expand Down Expand Up @@ -331,7 +331,7 @@ func (c *Cache) GetDevice(device string) *Device {
c.Lock()
defer c.Unlock()

c.refreshIfRequired(false)
_, _ = c.refreshIfRequired(false)

return c.devices[device]
}
Expand All @@ -343,7 +343,7 @@ func (c *Cache) ListDevices() []string {
c.Lock()
defer c.Unlock()

c.refreshIfRequired(false)
_, _ = c.refreshIfRequired(false)

for name := range c.devices {
devices = append(devices, name)
Expand All @@ -360,7 +360,7 @@ func (c *Cache) ListVendors() []string {
c.Lock()
defer c.Unlock()

c.refreshIfRequired(false)
_, _ = c.refreshIfRequired(false)

for vendor := range c.specs {
vendors = append(vendors, vendor)
Expand All @@ -380,7 +380,7 @@ func (c *Cache) ListClasses() []string {
c.Lock()
defer c.Unlock()

c.refreshIfRequired(false)
_, _ = c.refreshIfRequired(false)

for _, specs := range c.specs {
for _, spec := range specs {
Expand All @@ -400,7 +400,7 @@ func (c *Cache) GetVendorSpecs(vendor string) []*Spec {
c.Lock()
defer c.Unlock()

c.refreshIfRequired(false)
_, _ = c.refreshIfRequired(false)

return c.specs[vendor]
}
Expand Down Expand Up @@ -535,7 +535,7 @@ func (w *watch) watch(fsw *fsnotify.Watcher, m *sync.Mutex, refresh func() error
} else {
w.update(dirErrors)
}
refresh()
_ = refresh()
m.Unlock()

case _, ok := <-watch.Errors:
Expand Down