From ba154ffbe73082598abab6b720512f3bf0bf1689 Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Fri, 13 Feb 2015 16:54:16 +0100 Subject: [PATCH] net: withNetNS: attempt to revert net ns in case of error Before this patch, when withNetNS() returns with an error, the current net namespace is undefined. In order to make the API more readable, withNetNS() should always at least attempt to return in a defined namespace used before being called, even in case of errors. In practice, the only caller of withNetNS will handle the error case correctly by calling Teardown() so there should be no functional changes with this patch. --- networking/networking.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/networking/networking.go b/networking/networking.go index 41e677c..50cbf9e 100644 --- a/networking/networking.go +++ b/networking/networking.go @@ -250,6 +250,10 @@ func withNetNS(curNS, tgtNS *os.File, f func() error) error { } if err := f(); err != nil { + // Attempt to revert the net ns in a known state + if err := util.SetNS(curNS, syscall.CLONE_NEWNET); err != nil { + log.Printf("Cannot revert the net namespace: %v", err) + } return err }