Skip to content

Commit

Permalink
cobra: move engine shutdown to Execute
Browse files Browse the repository at this point in the history
If the run errors, cobra does not execute post runs.  It is a somehow
known issue (spf13/cobra#914) but problematic
for Podmand as the runtime is shutdown during post run.

Since some commands overwrite the post run and a general lack in cobra
of post runs on errors, move the shutting down the engines directly into
Execute.  Fixing the issue may fix a number of flakes.

Note that the shutdowns are NOPs for the remote client.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
  • Loading branch information
vrothberg committed Jan 10, 2023
1 parent bc6908e commit 2d8225c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cmd/podman/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ func Execute() {
}
fmt.Fprintln(os.Stderr, formatError(err))
}

if requireCleanup {
// The cobra post-run is not being executed in case of
// a previous error , so make sure that the engine(s)
// are correctly shutdown.
//
// See https://github.com/spf13/cobra/issues/914
logrus.Debugf("Shutting down engines")
if engine := registry.ImageEngine(); engine != nil {
engine.Shutdown(registry.Context())
}
if engine := registry.ContainerEngine(); engine != nil {
engine.Shutdown(registry.Context())
}
}

os.Exit(registry.GetExitCode())
}

Expand Down Expand Up @@ -298,13 +314,6 @@ func persistentPreRunE(cmd *cobra.Command, args []string) error {
func persistentPostRunE(cmd *cobra.Command, args []string) error {
logrus.Debugf("Called %s.PersistentPostRunE(%s)", cmd.Name(), strings.Join(os.Args, " "))

if !requireCleanup {
return nil
}

registry.ImageEngine().Shutdown(registry.Context())
registry.ContainerEngine().Shutdown(registry.Context())

if registry.IsRemote() {
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions test/system/001-basic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,11 @@ run_podman --noout system connection ls
is "$output" "" "output should be empty"
}

@test "podman - shutdown engines" {
run_podman --log-level=debug run --rm $IMAGE true
is "$output" ".*Shutting down engines.*"
run_podman 125 --log-level=debug run dockah://rien.de/rien:latest
is "${lines[-1]}" ".*Shutting down engines"
}

# vim: filetype=sh

0 comments on commit 2d8225c

Please sign in to comment.