From 317a88ee4e03a5434887830ec53c719be934e0ed Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sun, 14 Jul 2024 19:43:56 +0200 Subject: [PATCH 1/5] cmd: call shutdown handler stop function it is needed to wait for the handlers if they are currently being processed. Signed-off-by: Giuseppe Scrivano --- cmd/podman/root.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/podman/root.go b/cmd/podman/root.go index a373a3aa18..1ad2cc18be 100644 --- a/cmd/podman/root.go +++ b/cmd/podman/root.go @@ -16,6 +16,7 @@ import ( "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/validate" "github.com/containers/podman/v5/libpod/define" + "github.com/containers/podman/v5/libpod/shutdown" "github.com/containers/podman/v5/pkg/bindings" "github.com/containers/podman/v5/pkg/checkpoint/crutils" "github.com/containers/podman/v5/pkg/domain/entities" @@ -124,9 +125,11 @@ func Execute() { fmt.Fprintln(os.Stderr, formatError(err)) } + _ = shutdown.Stop() + if requireCleanup { // The cobra post-run is not being executed in case of - // a previous error , so make sure that the engine(s) + // a previous error, so make sure that the engine(s) // are correctly shutdown. // // See https://github.com/spf13/cobra/issues/914 From 6832a35f6592ca89380837c63954d7d499e0120b Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sun, 14 Jul 2024 19:44:18 +0200 Subject: [PATCH 2/5] libpod: cleanup store at shutdown shutdown the containers store so that the home directory mount is not leaked when "podman system service" exits. Signed-off-by: Giuseppe Scrivano --- libpod/runtime.go | 11 +++++++---- test/system/001-basic.bats | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/libpod/runtime.go b/libpod/runtime.go index 565a74645e..4c7d06f1f9 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -210,7 +210,14 @@ func newRuntimeFromConfig(ctx context.Context, conf *config.Config, options ...R } } + if err := makeRuntime(ctx, runtime); err != nil { + return nil, err + } + if err := shutdown.Register("libpod", func(sig os.Signal) error { + if runtime.store != nil { + _, _ = runtime.store.Shutdown(false) + } // For `systemctl stop podman.service` support, exit code should be 0 if sig == syscall.SIGTERM { os.Exit(0) @@ -225,10 +232,6 @@ func newRuntimeFromConfig(ctx context.Context, conf *config.Config, options ...R return nil, fmt.Errorf("starting shutdown signal handler: %w", err) } - if err := makeRuntime(ctx, runtime); err != nil { - return nil, err - } - runtime.config.CheckCgroupsAndAdjustConfig() return runtime, nil diff --git a/test/system/001-basic.bats b/test/system/001-basic.bats index 7c10296290..5cba515e9d 100644 --- a/test/system/001-basic.bats +++ b/test/system/001-basic.bats @@ -277,7 +277,7 @@ run_podman --noout system connection ls 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" + is "$output" ".*Shutting down engines.*" } # vim: filetype=sh From fbc4768a003e5dc95014e216e675b20b0fdd102f Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sun, 14 Jul 2024 19:44:43 +0200 Subject: [PATCH 3/5] libpod: shutdown Stop waits for handlers completion wait for handlers currently being processed. Signed-off-by: Giuseppe Scrivano --- libpod/shutdown/handler.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libpod/shutdown/handler.go b/libpod/shutdown/handler.go index 2b30a95768..d7fd5b5323 100644 --- a/libpod/shutdown/handler.go +++ b/libpod/shutdown/handler.go @@ -91,6 +91,11 @@ func Stop() error { return nil } + // if the signal handler is running, wait that it terminates + handlerLock.Lock() + defer handlerLock.Unlock() + // it doesn't need to be in the critical section, but staticcheck complains if + // the critical section is empty. cancelChan <- true return nil From e16e528f39a7cf6ceab648a6f6ca1ee24613e2d2 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sun, 14 Jul 2024 19:45:01 +0200 Subject: [PATCH 4/5] test: gracefully terminate server send a SIGTERM to the server process instead of killing it so it has time to do a proper cleanup and don't leak the home mount. Signed-off-by: Giuseppe Scrivano --- test/e2e/libpod_suite_remote_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/libpod_suite_remote_test.go b/test/e2e/libpod_suite_remote_test.go index 4c7a7c96f1..44a79eaf36 100644 --- a/test/e2e/libpod_suite_remote_test.go +++ b/test/e2e/libpod_suite_remote_test.go @@ -103,7 +103,7 @@ func (p *PodmanTestIntegration) StartRemoteService() { } func (p *PodmanTestIntegration) StopRemoteService() { - if err := p.RemoteSession.Kill(); err != nil { + if err := p.RemoteSession.Signal(syscall.SIGTERM); err != nil { GinkgoWriter.Printf("unable to clean up service %d, %v\n", p.RemoteSession.Pid, err) } if _, err := p.RemoteSession.Wait(); err != nil { From b08b630c84b07e765b2b5dd0053389b151001423 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Sun, 14 Jul 2024 15:05:38 +0200 Subject: [PATCH 5/5] test: drop unmount for overlay The unmount for the driver home dir is done automatically by the store on Shutdown. Do not do the unmount from the tests cleanup. Signed-off-by: Giuseppe Scrivano --- test/e2e/common_test.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/test/e2e/common_test.go b/test/e2e/common_test.go index a3c9c72df4..5e0d1df72b 100644 --- a/test/e2e/common_test.go +++ b/test/e2e/common_test.go @@ -33,7 +33,6 @@ import ( . "github.com/onsi/gomega" . "github.com/onsi/gomega/gexec" "github.com/sirupsen/logrus" - "golang.org/x/sys/unix" ) var ( @@ -1057,16 +1056,6 @@ func rmAll(podmanBin string, path string) { GinkgoWriter.Printf("%v\n", err) } } else { - // When using overlay as root, podman leaves a stray mount behind. - // This leak causes remote tests to take a loooooong time, which - // then causes Cirrus to time out. Unmount that stray. - overlayPath := path + "/root/overlay" - if _, err := os.Stat(overlayPath); err == nil { - if err = unix.Unmount(overlayPath, unix.MNT_DETACH); err != nil { - GinkgoWriter.Printf("Error unmounting %s: %v\n", overlayPath, err) - } - } - if err = os.RemoveAll(path); err != nil { GinkgoWriter.Printf("%q\n", err) }