From a9de888a15a6807bf98234bad22e623931f19e0b Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 5 Jun 2024 16:28:26 +0200 Subject: [PATCH] libpod: do not resuse networking on start If a container was stopped and we try to start it before we called cleanup it tried to reuse the network which caused a panic as the pasta code cannot deal with that. It is also never correct as the netns must be created by the runtime in case of custom user namespaces used. As such the proper thing is to clean the netns up first. Also change a e2e test to report better errors. It is not directly related to this chnage but it failed on v1 of this patch so we noticed the ugly error message it produced. Thanks to Ed for the fix. Signed-off-by: Paul Holzinger --- libpod/container_internal_linux.go | 8 ++++++++ test/e2e/restart_test.go | 6 ++++-- test/system/045-start.bats | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go index 42223587ef..a7b16a7f0a 100644 --- a/libpod/container_internal_linux.go +++ b/libpod/container_internal_linux.go @@ -71,6 +71,14 @@ func (c *Container) prepare() error { go func() { defer wg.Done() + if c.state.State == define.ContainerStateStopped { + // networking should not be reused after a stop + if err := c.cleanupNetwork(); err != nil { + createNetNSErr = err + return + } + } + // Set up network namespace if not already set up noNetNS := c.state.NetNS == "" if c.config.CreateNetNS && noNetNS && !c.config.PostConfigureNetNS { diff --git a/test/e2e/restart_test.go b/test/e2e/restart_test.go index 69401ad7c7..dcdd5f231c 100644 --- a/test/e2e/restart_test.go +++ b/test/e2e/restart_test.go @@ -193,12 +193,13 @@ var _ = Describe("Podman restart", func() { session.WaitWithDefaultTimeout() Expect(session).Should(ExitCleanly()) - testCmd := []string{"exec", "host-restart-test", "sh", "-c", "wc -l < /etc/hosts"} + testCmd := []string{"exec", "host-restart-test", "cat", "/etc/hosts"} // before restart beforeRestart := podmanTest.Podman(testCmd) beforeRestart.WaitWithDefaultTimeout() Expect(beforeRestart).Should(ExitCleanly()) + nHostLines := len(beforeRestart.OutputToStringArray()) session = podmanTest.Podman([]string{"restart", "host-restart-test"}) session.WaitWithDefaultTimeout() @@ -209,7 +210,8 @@ var _ = Describe("Podman restart", func() { Expect(afterRestart).Should(ExitCleanly()) // line count should be equal - Expect(beforeRestart.OutputToString()).To(Equal(afterRestart.OutputToString())) + Expect(afterRestart.OutputToStringArray()).To(HaveLen(nHostLines), + "number of host lines post-restart == number of lines pre-restart") }) It("podman restart all stopped containers with --all", func() { diff --git a/test/system/045-start.bats b/test/system/045-start.bats index b4840e76b1..52b2abc00d 100644 --- a/test/system/045-start.bats +++ b/test/system/045-start.bats @@ -58,6 +58,9 @@ load helpers is "$output" ".*$c1_id.*" "--filter finds container 1" is "$output" ".*$c3_id.*" "--filter finds container 3" + # start again, before this fix it could panic + run_podman start --filter restart-policy=always + # Start via filtered names run_podman start --filter restart-policy=on-failure $c2 $c3 is "$output" "$c2" "--filter finds container 2"