bugfix: have --{cpu,mem,...}-profile work even if Restic exits with error code (#5373)

* bugfix: write pprof file for `--{cpu,mem,...}-profile` even on error code

Before this, if `restic backup --cpu-profile dir/ backup-dir/` couldn't
read some of the input files (e.g. they weren't readable by the user
restic was running under), the `cpu.pprof` file it outputs would be
empty.

https://github.com/spf13/cobra/issues/1893

* drop changelog as it's not relevant for end users

---------

Co-authored-by: Michael Eischer <michael.eischer@fau.de>
This commit is contained in:
Ilya Grigoriev
2025-06-02 10:57:07 -07:00
committed by GitHub
parent e5bdc3c74f
commit 99ee5696f3

View File

@@ -30,14 +30,12 @@ func registerProfiling(cmd *cobra.Command) {
return profiler.Start(profiler.opts)
}
origPostRun := cmd.PersistentPostRunE
cmd.PersistentPostRunE = func(cmd *cobra.Command, args []string) error {
// Once https://github.com/spf13/cobra/issues/1893 is fixed,
// this could use PersistentPostRunE instead of OnFinalize,
// reverting https://github.com/restic/restic/pull/5373.
cobra.OnFinalize(func() {
profiler.Stop()
if origPostRun != nil {
return origPostRun(cmd, args)
}
return nil
}
})
profiler.opts.AddFlags(cmd.PersistentFlags())
}