@@ -260,34 +260,37 @@ func newNSPath(nsPath string) (ns.NetNS, error) {
260260// UnmountNS unmounts the given netns path
261261func 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