Skip to content

Commit e22db73

Browse files
committed
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
1 parent 428cee5 commit e22db73

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

cmd/root/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s
142142
if err != nil {
143143
return fmt.Errorf("failed to create CPU profile: %w", err)
144144
}
145-
defer pf.Close()
146145
if err := pprof.StartCPUProfile(pf); err != nil {
146+
pf.Close()
147147
return fmt.Errorf("failed to start CPU profile: %w", err)
148148
}
149149
defer pprof.StopCPUProfile()
150+
defer pf.Close()
150151
slog.Info("CPU profiling enabled", "file", f.cpuProfile)
151152
}
152153

0 commit comments

Comments
 (0)