@@ -358,6 +358,13 @@ func (wbs *InWorkspaceServiceServer) MountProc(ctx context.Context, req *api.Mou
358
358
return nil , err
359
359
}
360
360
361
+ // now that we've moved the mount (which we've done with OPEN_TREE_CLONE), we'll
362
+ // need to unmount the mask mounts again to not leave them dangling.
363
+ var masks []string
364
+ masks = append (masks , procDefaultMaskedPaths ... )
365
+ masks = append (masks , procDefaultReadonlyPaths ... )
366
+ cleanupMaskedMount (wbs .Session .OWI (), nodeStaging , masks )
367
+
361
368
return & api.MountProcResponse {}, nil
362
369
}
363
370
@@ -571,6 +578,8 @@ func (wbs *InWorkspaceServiceServer) MountSysfs(ctx context.Context, req *api.Mo
571
578
return nil , err
572
579
}
573
580
581
+ cleanupMaskedMount (wbs .Session .OWI (), nodeStaging , sysfsDefaultMaskedPaths )
582
+
574
583
return & api.MountProcResponse {}, nil
575
584
}
576
585
@@ -594,6 +603,38 @@ func moveMount(instanceID string, targetPid int, source, target string) error {
594
603
return nil
595
604
}
596
605
606
+ // cleanupMaskedMount will unmount and remove the paths joined with the basedir.
607
+ // Errors are logged instead of returned.
608
+ // This is useful for when we've moved the mount (which we've done with OPEN_TREE_CLONE), we'll
609
+ // need to unmount the mask mounts again to not leave them dangling.
610
+ func cleanupMaskedMount (owi map [string ]interface {}, base string , paths []string ) {
611
+ for _ , mask := range paths {
612
+ // Note: if errors happen while unmounting or removing the masks this does not mean
613
+ // that the final unmount won't happen. I.e. we can ignore those errors here
614
+ // because they would not be actionable anyways. Only if the final removal or
615
+ // unmount fails did we leak a mount.
616
+
617
+ fn := filepath .Join (base , mask )
618
+ err := unix .Unmount (fn , 0 )
619
+ if err != nil {
620
+ continue
621
+ }
622
+ _ = os .RemoveAll (fn )
623
+ }
624
+
625
+ err := unix .Unmount (base , 0 )
626
+ if err != nil {
627
+ log .WithError (err ).WithField ("fn" , base ).WithFields (owi ).Warn ("cannot unmount dangling base mount" )
628
+ return
629
+ }
630
+
631
+ err = os .RemoveAll (base )
632
+ if err != nil {
633
+ log .WithError (err ).WithField ("fn" , base ).WithFields (owi ).Warn ("cannot remove dangling base mount" )
634
+ return
635
+ }
636
+ }
637
+
597
638
type nsinsiderOpts struct {
598
639
MountNS bool
599
640
PidNS bool
0 commit comments