diff --git a/libpod/networking_common.go b/libpod/networking_common.go index a569e9eaed..24165e1598 100644 --- a/libpod/networking_common.go +++ b/libpod/networking_common.go @@ -115,7 +115,7 @@ func (r *Runtime) teardownNetworkBackend(ns string, opts types.NetworkOptions) e // execute the network setup in the rootless net ns err = rootlessNetNS.Do(tearDownPod) if cerr := rootlessNetNS.Cleanup(r); cerr != nil { - logrus.WithError(err).Error("failed to clean up rootless netns") + logrus.WithError(cerr).Error("failed to clean up rootless netns") } rootlessNetNS.Lock.Unlock() } else { diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go index 89eb6bbb08..13befa0b37 100644 --- a/libpod/networking_linux.go +++ b/libpod/networking_linux.go @@ -592,6 +592,14 @@ func (r *Runtime) configureNetNS(ctr *Container, ctrNS string) (status map[strin if err != nil { return nil, err } + defer func() { + // do not forget to tear down the netns when a later error happened. + if rerr != nil { + if err := r.teardownNetworkBackend(ctrNS, netOpts); err != nil { + logrus.Warnf("failed to teardown network after failed setup: %v", err) + } + } + }() // set up rootless port forwarder when rootless with ports and the network status is empty, // if this is called from network reload the network status will not be empty and we should