netns
netns copied to clipboard
DeleteNamed() - not clearing namedPath if mount does not exist
In some situations (e.g. program panic or abruptly terminated), a network namespace may be left unmounted, but with the namedPath still existing. I.e. the file system may be left with the file existing such as /run/netns/xyz
but not mounted.
Further, going via a sequence of DeleteNamed() followed by NewNamed() does not resolve the situation since DeleteNamed() will not remove the file if not mounted (See code below). There seem to be no way to get the code to work in such a situation. As a minimal quick solution, I suggest modifying DeleteNamed from:
func DeleteNamed(name string) error {
namedPath := path.Join(bindMountPath, name)
err := syscall.Unmount(namedPath, syscall.MNT_DETACH)
if err != nil {
return err
}
return os.Remove(namedPath)
}
to be:
func DeleteNamed(name string) error {
namedPath := path.Join(bindMountPath, name)
err := syscall.Unmount(namedPath, syscall.MNT_DETACH)
if err != nil {
os.Remove(namedPath)
return err
}
return os.Remove(namedPath)
}
As a longer term, more complete solution, I would suggest adding mutex to protect this from ever happaning.