From 99ee5696f3b1da280ff142e8b136d52741848e51 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Mon, 2 Jun 2025 10:57:07 -0700 Subject: [PATCH] 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 --- cmd/restic/global_debug.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/restic/global_debug.go b/cmd/restic/global_debug.go index 1fe35146a..e536bffea 100644 --- a/cmd/restic/global_debug.go +++ b/cmd/restic/global_debug.go @@ -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()) }