Skip to content

Commit c174769

Browse files
Merge pull request #2239 from Luap99/unmount-netns
pkg/netns: add 60s timeout
2 parents 9819e05 + ef5388b commit c174769

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

pkg/netns/netns_linux.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -260,34 +260,37 @@ func newNSPath(nsPath string) (ns.NetNS, error) {
260260
// UnmountNS unmounts the given netns path
261261
func UnmountNS(nsPath string) error {
262262
// Only unmount if it's been bind-mounted (don't touch namespaces in /proc...)
263-
if !strings.HasPrefix(nsPath, "/proc/") {
264-
// EINVAL means the path exists but is not mounted, just try to remove the path below
265-
if err := unix.Unmount(nsPath, unix.MNT_DETACH); err != nil && !errors.Is(err, unix.EINVAL) {
266-
// If path does not exists we can return without error as we have nothing to do.
267-
if errors.Is(err, unix.ENOENT) {
268-
return nil
269-
}
270-
271-
return fmt.Errorf("failed to unmount NS: at %s: %w", nsPath, err)
263+
if strings.HasPrefix(nsPath, "/proc/") {
264+
return nil
265+
}
266+
// EINVAL means the path exists but is not mounted, just try to remove the path below
267+
if err := unix.Unmount(nsPath, unix.MNT_DETACH); err != nil && !errors.Is(err, unix.EINVAL) {
268+
// If path does not exists we can return without error as we have nothing to do.
269+
if errors.Is(err, unix.ENOENT) {
270+
return nil
272271
}
273272

274-
for {
275-
if err := os.Remove(nsPath); err != nil {
276-
if errors.Is(err, unix.EBUSY) {
277-
// mount is still busy, sleep a moment and try again to remove
278-
logrus.Debugf("Netns %s still busy, try removing it again in 10ms", nsPath)
279-
time.Sleep(10 * time.Millisecond)
280-
continue
281-
}
282-
// If path does not exists we can return without error.
283-
if errors.Is(err, unix.ENOENT) {
284-
break
285-
}
286-
return fmt.Errorf("failed to remove ns path: %w", err)
273+
return fmt.Errorf("failed to unmount NS: at %s: %w", nsPath, err)
274+
}
275+
276+
var err error
277+
// wait for up to 60s in the loop
278+
for range 6000 {
279+
if err = os.Remove(nsPath); err != nil {
280+
if errors.Is(err, unix.EBUSY) {
281+
// mount is still busy, sleep a moment and try again to remove
282+
logrus.Debugf("Netns %s still busy, try removing it again in 10ms", nsPath)
283+
time.Sleep(10 * time.Millisecond)
284+
continue
287285
}
288-
break
286+
// If path does not exists we can return without error.
287+
if errors.Is(err, unix.ENOENT) {
288+
return nil
289+
}
290+
return fmt.Errorf("failed to remove ns path: %w", err)
289291
}
292+
return nil
290293
}
291294

292-
return nil
295+
return fmt.Errorf("failed to remove ns path (timeout after 60s): %w", err)
293296
}

0 commit comments

Comments
 (0)