From f0eca1afc2477dfcf0f17078e902e59956af63c2 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 28 Mar 2026 21:55:51 +0700 Subject: internal/cgrouptest: use t.Cleanup instead of defer in InCgroupV2 Using defer may cause the TestCgroupGOMAXPROCS fail with the following error messages, --- FAIL: TestCgroupGOMAXPROCS (5.14s) crash_test.go:193: running /home/ms/src/go/bin/go build -o /tmp/go-build2154548206/testprog.exe crash_test.go:216: built testprog in 5.13507196s --- FAIL: TestCgroupGOMAXPROCS/containermaxprocs=0 (0.00s) cgroup_linux_test.go:60: /tmp/go-build2154548206/testprog.exe PrintGOMAXPROCS (1.191505ms): ok cgrouptest_linux.go:81: Error removing cgroup directory: remove /sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/go-cgrouptest4138918176: device or resource busy Under the following environment, - Linux 6.19.9-arch1-1 #1 SMP PREEMPT_DYNAMIC - systemd 260 (260.1-1-arch) Change-Id: Ic36b0b98f0d589ea5730ea35097af04a22dc7469 --- src/internal/cgrouptest/cgrouptest_linux.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/internal/cgrouptest/cgrouptest_linux.go b/src/internal/cgrouptest/cgrouptest_linux.go index ad9599c938..72380debc6 100644 --- a/src/internal/cgrouptest/cgrouptest_linux.go +++ b/src/internal/cgrouptest/cgrouptest_linux.go @@ -74,16 +74,16 @@ func InCgroupV2(t *testing.T, fn func(*CgroupV2)) { // // TODO(prattmic): Consider running everything in a subprocess just so // we can clean up if it throws or otherwise doesn't run the defers. - defer func() { + t.Cleanup(func() { if err := os.Remove(path); err != nil { // Not much we can do, but at least inform of the // problem. t.Errorf("Error removing cgroup directory: %v", err) } - }() + }) migrateTo(t, path) - defer migrateTo(t, orig) + t.Cleanup(func() { migrateTo(t, orig) }) c := &CgroupV2{ orig: orig, -- cgit v1.3