From e22db736cd1c4638d8fcd11f7785cbf40e03834f Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 19 Feb 2026 09:55:54 +0100 Subject: [PATCH 1/2] fix: close CPU profile file immediately on StartCPUProfile error When pprof.StartCPUProfile fails, the file was left to be closed by a deferred call placed before the error check. This could shadow the original error and delay resource cleanup. Now the file is closed explicitly on error, and defer is placed after the success path. Fixes #1770 Fixes #1765 Assisted-By: cagent --- cmd/root/run.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/root/run.go b/cmd/root/run.go index 9cf76fad1..cf98f3879 100644 --- a/cmd/root/run.go +++ b/cmd/root/run.go @@ -142,11 +142,12 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s if err != nil { return fmt.Errorf("failed to create CPU profile: %w", err) } - defer pf.Close() if err := pprof.StartCPUProfile(pf); err != nil { + pf.Close() return fmt.Errorf("failed to start CPU profile: %w", err) } defer pprof.StopCPUProfile() + defer pf.Close() slog.Info("CPU profiling enabled", "file", f.cpuProfile) } From ce0795bc89bd574accf0e7e49e38d2b319d7d58c Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 19 Feb 2026 09:56:18 +0100 Subject: [PATCH 2/2] fix: handle error from Close() in isFirstRun The f.Close() call was ignoring the returned error. Now we log a warning if closing the first-run marker file fails. Fixes #1769 Assisted-By: cagent --- cmd/root/root.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/root/root.go b/cmd/root/root.go index cd594e490..a97b61160 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -311,7 +311,9 @@ func isFirstRun() bool { if err != nil { return false // File already exists or other error, not first run } - f.Close() + if err := f.Close(); err != nil { + slog.Warn("Failed to close first run marker file", "error", err) + } return true }