@@ -853,6 +853,10 @@ func testCgroupParent(t *testing.T, sb integration.Sandbox) {
853853 t .SkipNow ()
854854 }
855855
856+ if _ , err := os .Lstat ("/sys/fs/cgroup/cgroup.subtree_control" ); os .IsNotExist (err ) {
857+ t .Skipf ("test requires cgroup v2" )
858+ }
859+
856860 c , err := New (sb .Context (), sb .Address ())
857861 require .NoError (t , err )
858862 defer c .Close ()
@@ -864,8 +868,21 @@ func testCgroupParent(t *testing.T, sb integration.Sandbox) {
864868 st = img .Run (append (ro , llb .Shlex (cmd ), llb .Dir ("/wd" ))... ).AddMount ("/wd" , st )
865869 }
866870
867- run (`sh -c "cat /proc/self/cgroup > first"` , llb .WithCgroupParent ("foocgroup" ))
868- run (`sh -c "cat /proc/self/cgroup > second"` )
871+ cgroupName := "test." + identity .NewID ()
872+
873+ err = os .MkdirAll (filepath .Join ("/sys/fs/cgroup" , cgroupName ), 0755 )
874+ require .NoError (t , err )
875+
876+ defer func () {
877+ err := os .RemoveAll (filepath .Join ("/sys/fs/cgroup" , cgroupName ))
878+ require .NoError (t , err )
879+ }()
880+
881+ err = os .WriteFile (filepath .Join ("/sys/fs/cgroup" , cgroupName , "pids.max" ), []byte ("10" ), 0644 )
882+ require .NoError (t , err )
883+
884+ run (`sh -c "(for i in $(seq 1 10); do sleep 1 & done 2>first.error); cat /proc/self/cgroup >> first"` , llb .WithCgroupParent (cgroupName ))
885+ run (`sh -c "(for i in $(seq 1 10); do sleep 1 & done 2>second.error); cat /proc/self/cgroup >> second"` )
869886
870887 def , err := st .Marshal (sb .Context ())
871888 require .NoError (t , err )
@@ -882,13 +899,22 @@ func testCgroupParent(t *testing.T, sb integration.Sandbox) {
882899 }, nil )
883900 require .NoError (t , err )
884901
902+ // neither process leaks parent cgroup name inside container
885903 dt , err := os .ReadFile (filepath .Join (destDir , "first" ))
886904 require .NoError (t , err )
887- require .Contains (t , strings .TrimSpace (string (dt )), `/foocgroup/buildkit/` )
905+ require .NotContains (t , strings .TrimSpace (string (dt )), cgroupName )
888906
889907 dt2 , err := os .ReadFile (filepath .Join (destDir , "second" ))
890908 require .NoError (t , err )
891- require .NotContains (t , strings .TrimSpace (string (dt2 )), `/foocgroup/buildkit/` )
909+ require .NotContains (t , strings .TrimSpace (string (dt2 )), cgroupName )
910+
911+ dt , err = os .ReadFile (filepath .Join (destDir , "first.error" ))
912+ require .NoError (t , err )
913+ require .Contains (t , strings .TrimSpace (string (dt )), "Resource temporarily unavailable" )
914+
915+ dt , err = os .ReadFile (filepath .Join (destDir , "second.error" ))
916+ require .NoError (t , err )
917+ require .Equal (t , strings .TrimSpace (string (dt )), "" )
892918}
893919
894920func testNetworkMode (t * testing.T , sb integration.Sandbox ) {
0 commit comments