diff --git a/cmd/nvidia-ctk/config/config.go b/cmd/nvidia-ctk/config/config.go index ee5832b6..f6d636fc 100644 --- a/cmd/nvidia-ctk/config/config.go +++ b/cmd/nvidia-ctk/config/config.go @@ -119,10 +119,10 @@ func run(c *cli.Context, opts *options) error { } defer output.Close() - if err != nil { - return err + if _, err := cfgToml.Save(output); err != nil { + return fmt.Errorf("failed to save config: %v", err) } - cfgToml.Save(output) + return nil } diff --git a/cmd/nvidia-ctk/config/create-default/create-default.go b/cmd/nvidia-ctk/config/create-default/create-default.go index d8c8b00f..e16eb855 100644 --- a/cmd/nvidia-ctk/config/create-default/create-default.go +++ b/cmd/nvidia-ctk/config/create-default/create-default.go @@ -85,8 +85,7 @@ func (m command) run(c *cli.Context, opts *flags.Options) error { } defer output.Close() - _, err = cfgToml.Save(output) - if err != nil { + if _, err = cfgToml.Save(output); err != nil { return fmt.Errorf("failed to write output: %v", err) } diff --git a/internal/info/additional_info.go b/internal/info/additional_info.go index fbb9ed23..9fffad78 100644 --- a/internal/info/additional_info.go +++ b/internal/info/additional_info.go @@ -52,7 +52,9 @@ func (i additionalInfo) UsesNVGPUModule() (uses bool, reason string) { if ret != nvml.SUCCESS { return false, fmt.Sprintf("failed to initialize nvml: %v", ret) } - defer i.nvmllib.Shutdown() + defer func() { + _ = i.nvmllib.Shutdown() + }() var names []string diff --git a/internal/oci/spec_test.go b/internal/oci/spec_test.go index f17f3d3c..1acf1728 100644 --- a/internal/oci/spec_test.go +++ b/internal/oci/spec_test.go @@ -22,7 +22,8 @@ func TestMaintainSpec(t *testing.T) { spec := NewFileSpec(inputSpecPath).(*fileSpec) - spec.Load() + _, err := spec.Load() + require.NoError(t, err) outputSpecPath := filepath.Join(moduleRoot, "test/output", f) spec.path = outputSpecPath diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index e43c778a..87a11f6a 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -18,6 +18,7 @@ package runtime import ( "encoding/json" + "errors" "fmt" "strings" @@ -53,7 +54,9 @@ func (r rt) Run(argv []string) (rerr error) { if rerr != nil { r.logger.Errorf("%v", rerr) } - r.logger.Reset() + if err := r.logger.Reset(); err != nil { + rerr = errors.Join(rerr, fmt.Errorf("failed to reset logger: %v", err)) + } }() // We apply some config updates here to ensure that the config is valid in diff --git a/pkg/nvcdi/driver-nvml.go b/pkg/nvcdi/driver-nvml.go index 89349b27..28dacae3 100644 --- a/pkg/nvcdi/driver-nvml.go +++ b/pkg/nvcdi/driver-nvml.go @@ -36,7 +36,11 @@ func NewDriverDiscoverer(logger logger.Interface, driverRoot string, nvidiaCTKPa if r := nvmllib.Init(); r != nvml.SUCCESS { return nil, fmt.Errorf("failed to initialize NVML: %v", r) } - defer nvmllib.Shutdown() + defer func() { + if r := nvmllib.Shutdown(); r != nvml.SUCCESS { + logger.Warningf("failed to shutdown NVML: %v", r) + } + }() version, r := nvmllib.SystemGetDriverVersion() if r != nvml.SUCCESS { diff --git a/pkg/nvcdi/driver-wsl.go b/pkg/nvcdi/driver-wsl.go index 1aa02b8e..c49ef4ad 100644 --- a/pkg/nvcdi/driver-wsl.go +++ b/pkg/nvcdi/driver-wsl.go @@ -43,7 +43,11 @@ func newWSLDriverDiscoverer(logger logger.Interface, driverRoot string, nvidiaCT if err != nil { return nil, fmt.Errorf("failed to initialize dxcore: %v", err) } - defer dxcore.Shutdown() + defer func() { + if err := dxcore.Shutdown(); err != nil { + logger.Warningf("failed to shutdown dxcore: %v", err) + } + }() driverStorePaths := dxcore.GetDriverStorePaths() if len(driverStorePaths) == 0 { diff --git a/pkg/nvcdi/lib-nvml.go b/pkg/nvcdi/lib-nvml.go index 1a43cdb7..db6cabab 100644 --- a/pkg/nvcdi/lib-nvml.go +++ b/pkg/nvcdi/lib-nvml.go @@ -43,7 +43,11 @@ func (l *nvmllib) GetAllDeviceSpecs() ([]specs.Device, error) { if r := l.nvmllib.Init(); r != nvml.SUCCESS { return nil, fmt.Errorf("failed to initialize NVML: %v", r) } - defer l.nvmllib.Shutdown() + defer func() { + if r := l.nvmllib.Shutdown(); r != nvml.SUCCESS { + l.logger.Warningf("failed to shutdown NVML: %v", r) + } + }() gpuDeviceSpecs, err := l.getGPUDeviceSpecs() if err != nil { diff --git a/pkg/nvcdi/lib.go b/pkg/nvcdi/lib.go index f8a52c1b..be4e62af 100644 --- a/pkg/nvcdi/lib.go +++ b/pkg/nvcdi/lib.go @@ -191,7 +191,11 @@ func (l *nvcdilib) getCudaVersion() (string, error) { if r != nvml.SUCCESS { return "", fmt.Errorf("failed to initialize nvml: %v", r) } - defer l.nvmllib.Shutdown() + defer func() { + if r := l.nvmllib.Shutdown(); r != nvml.SUCCESS { + l.logger.Warningf("failed to shutdown NVML: %v", r) + } + }() version, r := l.nvmllib.SystemGetDriverVersion() if r != nvml.SUCCESS { diff --git a/tools/container/toolkit/toolkit.go b/tools/container/toolkit/toolkit.go index 971916ae..93766b23 100644 --- a/tools/container/toolkit/toolkit.go +++ b/tools/container/toolkit/toolkit.go @@ -455,13 +455,14 @@ func installToolkitConfig(c *cli.Context, toolkitConfigPath string, nvidiaContai config.Set(key, value) } - _, err = config.WriteTo(targetConfig) - if err != nil { + if _, err := config.WriteTo(targetConfig); err != nil { return fmt.Errorf("error writing config: %v", err) } os.Stdout.WriteString("Using config:\n") - config.WriteTo(os.Stdout) + if _, err = config.WriteTo(os.Stdout); err != nil { + log.Warningf("Failed to output config to STDOUT: %v", err) + } return nil }